• Post Reply Bookmark Topic Watch Topic
  • New Topic

Help with Factory Design Pattern?  RSS feed

 
Dan Pants
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


 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • 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


 
Rico Felix
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?
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!