Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Syntax Problem?  RSS feed

 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need a pair of new eyes as I cannot see what appears to me to be some kind of syntax problem. I keep getting a null pointer exception on line 90 which does not exist in my class:


Here is the stacktrace:

java.lang.NullPointerException
at StokerMonitor.RampClass.SetRamps(RampClass.java:90)
at StokerMonitor.RampClass.initialize(RampClass.java:58)
at StokerMonitor.RampClass$1.run(RampClass.java:28)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you execute the code? It does not have a main method.

Somewhere there must be another version of the code with matching line numbers.
Scan the end of the code for any variables that have not been given a value and would be null when the code is executed.
Make sure all variables used at the end of the code have been assigned a value.
 
Knute Snortum
Sheriff
Posts: 4073
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when ramps is null? It looks like line 83 is the problem.
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your == null tests allow something to be null and that can cause the NPE. You can find yourself trying to use ramps before it has been initialised, which is not a good thing to do. There is no way to make anybody call that method and initialise ramps before it is used. Also there is no way to stop people calling that method twice, and who knows what will go wrong then.
I am also suspicious because I can see that test is inside a static method, which makes me suspect you have made things static which ought not to be static.
I think you should initialise every field in the constructor; you can do it like this if you wish:-
ramp = getRamp(...);
but the null test will have to be removed. Initialising everything to non‑null in the constructor will go a long way towards obliterating nulls in your program.
 
Dennis Putnam
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies. I'm a little fuzzy on the difference but I changed the List to an ArrayList, instantiated it and that solved the problem.
 
Campbell Ritchie
Marshal
Posts: 55698
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dennis Putnam wrote:Thanks for the replies.
That's a pleasure
. . . I changed the List to an ArrayList, instantiated it and that solved the problem.
It was the instantiation that solved the problem, not the change from List to ArrayList. Change the declaration back to List, and keep the instantiation unchanged; that should work.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!