• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Help with Factory Design Pattern?

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy ranchers! I'm sorry I've posted a similar question before that you all helped me with, but I still haven't been able to completely figure it out. My assignment was to create a simple form that demonstrates the use of the factory and singleton design patterns. "Use the Factory pattern to ensure that each form input consists of a text label and a text field. Use the Singleton pattern for the submit button."

My current issue is I'm getting a NullPointerException when it's calling the add() methods in the JavaForm.java. Any hints, advice or help on what I can do or try to get this working would be much appreciated.

Here's my code:


Form.java file


Name.java file (I have similar Address.java, city.java, state.java, zip.java, phone.java files I won't post here)



JavaForm.java file


 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at your class JavaForm. It has a number of member variables (lines 11-42). You're not initializing those variables explicitly, so they are initialized with the default value null.

Then, in the constructor, you call add(...) in lines 55-61, but all the variables that you are calling it with are null.

Make sure you initialize the member variables to non-null values before you add them.
 
Dan Pants
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks that helped a lot. I'm sorry I'm so clueless, I think this assignment is a bit over my head. I had a lot of code I didn't need.

I tried to simplify this but still can't get what I'm looking for. It compiles and runs now but it's blank, with no Name text box? What I am missing?


JavaForm.java



Name.java file


Form.java


 
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you observe the code snippets in your last post carefully, you would realize that your design and implementation are not mapping correctly to logical reasoning...

Your JavaForm1 is a JFrame which can have components added to it...

In the JavaForm1 constructor you are first setting up the title, size, exit-operation, and layout for the frame... Next you are created a local reference to an object of type Form and calling a method on this local reference then construction of the JavaForm1 is completed... You have not yet added a single component to it therefore the frame only has a title with its set size, exit-operation and layout property, hence its a completely astonishing 850x200 blank frame...
 
Dan Pants
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So doesn't the local reference to the Form object build and add to my JFrame?
 
Marshal
Posts: 15892
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When you instantiate a form with this constructor, all the variables that are passed on lines 54-61 are still null, that's why you get a NullPointerException.
 
He's giving us the slip! Quick! Grab this tiny ad!
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic