• Post Reply Bookmark Topic Watch Topic
  • New Topic

Setting variables between classes  RSS feed

 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I am having trouble with creating variable between class files.   My MainClass (TestMainClass.java) loads a properties file and gets the specific properties I want from it.   From my MainClass , I call a method from a different class (CommonProperties.setPropertyValues() ) and pass the Properties object to it.   If I initialize the variables within the setPropertyValue method I do not have any problems .   It's when I initialize them as fields in the class which I seem to have an issue.  I tried using getters and setters ( setting the variables within the setPropertyValue method) so I'm not sure what to do.

When I execute the jar file I receive a nullPointerException.   Can someone please help me?   Thank You












}



[/code]
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I receive a nullPointerException

Please copy the full text of the error message and paste it here. It has important info about the error.

Look at the source line shown in the error message and find the variable with the null value.  Then backtrack in the code to see why that variable does not have a valid value.
 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
I receive a nullPointerException

Please copy the full text of the error message and paste it here. It has important info about the error.

Look at the source line shown in the error message and find the variable with the null value.  Then backtrack in the code to see why that variable does not have a valid value.


Here is the full message

$ java -jar build/libs/ztb-ck-1.0-SNAPSHOT.jar build/
Exception in thread "main" java.lang.NullPointerException
        at com.TestMainClass.main(TestMainClass.java:40)

 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:
I receive a nullPointerException

Please copy the full text of the error message and paste it here. It has important info about the error.

Look at the source line shown in the error message and find the variable with the null value.  Then backtrack in the code to see why that variable does not have a valid value.


I responded in another post with the full message .   I did backtrack in the code and I tried a few things to figure this out but at this point I'm not really sure which is why I'm asking.  Sorry if I didn't come off like I tried already.
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Exception in thread "main" java.lang.NullPointerException
        at com.TestMainClass.main(TestMainClass.java:40)

The lines in the posted code don't seem to correspond with that in the error message.  There isn't a statement at line 40.
Have you looked at line 40?  What variable has the null value?  Add a print statement before line 40 that prints the variables used on line 40 if you can't tell which one is null.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

On line 71, setProp is a local variable which goes out of scope at the end of the method. Somehow you ended up with a static method, which doesn't look right, which then didn't allow you to set the class'es field variables.
 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Norm Radder wrote:

Exception in thread "main" java.lang.NullPointerException
        at com.TestMainClass.main(TestMainClass.java:40)

The lines in the posted code don't seem to correspond with that in the error message.  There isn't a statement at line 40.
Have you looked at line 40?  What variable has the null value?  Add a print statement before line 40 that prints the variables used on line 40 if you can't tell which one is null.


It corresponds with



in my code.  I commented in the copied and pasted code that it removed some unrelated code.  I did already look at the code.  From what I can tell this has something to do with how I'm handling the variables in the setPropertyValues method.  If I change storeProp.setPropertyValues(prop); to point to  CommonPropertiesWorks.java I get back the expected result. 
 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lisa Austin wrote:
Norm Radder wrote:

Exception in thread "main" java.lang.NullPointerException
        at com.TestMainClass.main(TestMainClass.java:40)

The lines in the posted code don't seem to correspond with that in the error message.  There isn't a statement at line 40.
Have you looked at line 40?  What variable has the null value?  Add a print statement before line 40 that prints the variables used on line 40 if you can't tell which one is null.


It corresponds with



in my code.  I commented in the copied and pasted code that it removed some unrelated code.  I did already look at the code.  From what I can tell this has something to do with how I'm handling the variables in the setPropertyValues method.  If I change storeProp.setPropertyValues(prop); to point to  CommonPropertiesWorks.java I get back the expected result. 


Correction if I change it to

  I get back the expected result.

If I use


even if there were print lines in the method setPropertyValues of CommonProperties it doesn't work.   If you look there are print statements in setPropertyValues of CommonPropertiesWorks file.     I know right NOW there are no print statement in CommonProperties but there has been.  This is a bit hard to explain because I have tried several things before I came here to ask for help.

I don't think the issue is with my MainClass.  It's with my CommonProperties file . 
 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
On line 71, setProp is a local variable which goes out of scope at the end of the method. Somehow you ended up with a static method, which doesn't look right, which then didn't allow you to set the class'es field variables.


Maybe a different issue or I'm looking at this the wrong way.

When I call the setPropertyValues method in CommonPropertiesWorks, it works as a static method but does NOT work when I take the static off. 

I removed the static setPropertyValues method of CommonPoperties and that did not work.







 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the output I received when I call the CommonPropertiesWorks.setPropertyValues() as a static method. 


This is expected result
$ java -jar build/libs/ztb-ck-1.0-SNAPSHOT.jar build/
New value authServerProp
New value ztbBaseProp
New  value false
New  value true

From the Main method
 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well I changed it up a bit and receiving different results.

I'm using


Received

$ java -jar build/libs/ztb-ck-1.0-SNAPSHOT.jar build/
New value null
New value null
New  value null
New  value null
here is propsnull

So I'll keep playing with it. 
 
Lisa Austin
Ranch Hand
Posts: 178
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay using this rather than trying to use the setter works.    Should I use a constructor instead?

 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am finding it very hard to follow this thread.  There are so many versions of the code and different errors.  Could you post your current code and state any problems you're having with it?
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line four in the above code seems to do nothing.  What was the intended use for that variable?
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay using this rather than trying to use the setter works.    Should I use a constructor instead? 

In general, using a constructor or using setters is a bit of a judgement call.  Most people say that if your constructor has more than three or four parameters, you should use setters.  You could also lookup java builder pattern to see yet another way to deal with objects that need a lot of parameters set at once.  But note that a setter usually looks like this:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!