• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cannot isolate this NullPointerException

 
Steven YaegerII
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a class Grapher2.java that I'm trying to remodel so that it is more OO and less procedural. The other related classes are Graph2.java & GraphFrame2.java, though I'm getting the NullPointerException from class Grapher2.java. I know that they aren't a very good representation of OOP, but does anyone see where I am messing up?? I thought I was doing something until runtime...
Exception in thread "main" java.lang.NullPointerException
at Grapher2.init(Grapher2.java:22)
at Grapher2.main(Grapher2.java:9)





Appreciatively
Steve
 
Steven YaegerII
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought I was close to catching the problem about six times, but then it would have been too easy. I've been using the below code to try and weed out the problem. Is my original program (above) just too lame, or is the error maybe too hard to trace at first glance?


I thought everything was ok after I changed i < numVLines & HLines to i == numVLines & HLines, because Graph2Test.java compiled and ran with no problems. But, when I made the changes in the real usable code (Graph2.java, etc, etc), I'm still getting the same NullPointerException. What is going on??? I cannot seem to isolate the problem because of problems such as the line above: System.out.println(graph.vBottom[139]; I think the problem is in the if statements, because those variable values are the ones that I cannot confirm the value of to see if they are even being created. I guess that the NullPointerException is being thrown because I'm trying to reference an object that isn't fully created???
I tried numerous other things also, compiling with -g, etc., etc. What can I be overlooking???
Appreciatively, Steve
I don't know how I accomplished this eternal scrolling, but it's a first. very sorry I couldn't fix it.
[This message has been edited by Steven YaegerII (edited April 12, 2001).]
[This message has been edited by Steven YaegerII (edited April 12, 2001).]
Inside ubb code blocks, lines do not wrap. When you put the long explaination in the comments of the without new lines, it spread everything out. I moved that note outside the code block--Carl
[This message has been edited by Carl Trusiak (edited April 12, 2001).]
 
Carl Trusiak
Sheriff
Posts: 3341
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steven YaegerII:

I think the init() is causing your problem. your class variable graph is being masked by the local variable graph.

This creates a Graph2 Object that is local to init and goes out of scope when the init finishes. Therefore your class variable

never gets created.
Change line 22 to

------------------
Hope This Helps
Carl Trusiak, SCJP2
[This message has been edited by Carl Trusiak (edited April 12, 2001).]
 
Sean MacLean
author
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is actually a rather quirky one. It's happening because of two errors and wouldn't occur unless both of these conditions exist (kinda neat actually). Firstly, you have to initialize the Point2D and Line2D arrays before populating them.
vBottom = new Point2D.Double[(int) numVLines];
vTop = new Point2D.Double[(int) numVLines];
vertLine = new Line2D.Double[(int) numVLines];
This would have caused a null pointer exception except another error was preventing this from occuring. In the for statement you should be checking the condition
for( int i = 0; i <= (int) numVLines; i++ ) {...}
where the key is the <= instead of == (which is false immediately so the null pointer mentioned above is never thrown because the loop is never entered). Anyway, fixing these two things should do it.
Sean
 
Steven YaegerII
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a million guys. That long comment inside the code tags was the result of fatigue. Now that I'm refreshed, it was pretty silly to even make it. Thanks again for your comments. I've taken it upon myself to stick with this tiny project to the end, in hopes that I may learn OO Java to a comfortable degree. I'm surprised and thrilled that you guys didn't say that the code was just all wrong. It seems that a little insight comes after every long period of frustration. It also helps that the prog will be useful, and therefore keep me enthusiastic through the rough times. I'm also getting pretty intimate with the code.
Back to the drawing board with renewed fervor.
Thanks guys! Steve
 
Steven YaegerII
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again, guys. Shawn, those initializations made sense once I seen them, but I don't think I would've ever thought of em. Carl, I should've seen that my local variable graph was masking my class variable graph. It works ...on to the next step.
Steve
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!