• Post Reply Bookmark Topic Watch Topic
  • New Topic

Struggling with Factory Design Pattern  RSS feed

 
Dan Pants
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy all. Posted a few similar questions about this a few weeks ago, but I just can't seem to get it right! 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."

I am holding off on the singleton submit button for now and just trying to get the factory design pattern for the form fields right. The problem I'm having at the moment is when its calling the getFormField methods from the JavaForm.java file. I'm getting the cannot call non-static method from static context, but it also won't let me make these methods static.


Perhaps I am completely missing or over thinking the concept of these design patterns but I seem to be struggling with them mightily. Any help, hints or advice you can give me would be much appreciated. Thank you!


Here is what I have:

file Form.java



file Name.java (I have also have similar files for Address, City, State, Zip and Phone which I won't post here to save room)




FormFactory.java file



file JavaForm.java


 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dan Pants wrote:


What you are doing here is first call FormFactory.getForm(...) to create some kind of Form object (in this case it's a Name object) and then in the second line you are calling buildFormField on class Name as if it is a static method.

What you probably wanted to do is call this on the object that you just created: name.buildFormField();

Note the difference with what you wrote: Name.buildFormField();

Also, you are passing lower-case names such as "name" to the FormFactory.getForm(...) method, but that method checks for upper-case names such as "NAME". "name" is not the same as "NAME" so it's not going to work.
 
Dan Pants
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jesper! Made the changes you suggest it everything compiles OK, but when I run it I'm getting a NullPointerException in the main method. Any idea what I'm missing?

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The stack trace of the NullPointerException tells you exactly in which line of your code the exception happens.

Look at that line. Something is null there, and you're trying to call a method on it or access a member variable on it. Trying to call a method or access a member variable on something that's null causes a NullPointerException.
 
Dan Pants
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again for replying Jesper. The NullPointerException is happening starting with this block of code



Any idea what I'm missing?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!