aspose file tools*
The moose likes Java in General and the fly likes Exception in thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Exception in thread "main" java.lang.NullPointerException" Watch "Exception in thread "main" java.lang.NullPointerException" New topic
Author

Exception in thread "main" java.lang.NullPointerException

William Koch
Ranch Hand

Joined: Sep 26, 2012
Posts: 76
This is not the whole program. But basically the program takes a <path> tag (or multiple path tags) from a scalable vector graphic file and converts it to a string and then removes n% unimportant points that the user decides.
Thanks to all those who can help!

This is the error that I am receiving when I go to test my code with an actual SVG file:

Exception in thread "main" java.lang.NullPointerException
at SVG.<init>(SVG.java:54)
at EditSVG.main(EditSVG.java:8)


I did some research and found that this error is caused by probably one of these three:
- Attempting to invoke an instance method of a null object
- Attempting to access or modify a particular field of a null object
- Attempting to obtain the length of such null object as an array

I have failed to locate the problem. Any suggestions on where in this code I can look for the problem and possibly a hint on what the specific problem is?

Here is a code snippet that ends on line 54 of my SVG.java file



Here is a code snippet from my EditSVG.java:




Thanks again - Will>
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

If this line is causing the exception:


then Paths is null, since that's the only reference you dereference in that line.

As a side note, Java convention is to begin variable names with a lowercase letter.
William Koch
Ranch Hand

Joined: Sep 26, 2012
Posts: 76
If Paths is null why can I not add something to it. I declared Paths in line 6. Is there a simple fix? Do I need to give Paths some initial value?

thanks for the naming convention tip! :-)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

William Koch wrote:If Paths is null why can I not add something to it.


Because it's null. The Paths variable holds a reference whose value is null--that is, a reference that doesn't point to any object. You have to create your ArrayList object before you can call methods on it. You can't call methods on a null reference.

I declared Paths in line 6.


Declaring a variable != creating an object.

Is there a simple fix?


Yup.

Do I need to give Paths some initial value?


You have to give it a non-null value before you dereference it. The most appropriate place in this case is probably the line where you declare it, but it doesn't have to be there.

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

And welcome to the Ranch!
William Koch
Ranch Hand

Joined: Sep 26, 2012
Posts: 76
How does this look?



I am new to Java if you haven't noticed. My first language was Ada95 but now I am having to learn Java as they try to teach me data structures. I am in my second programming course for my undergrad in Computer Science (it is a Data Structures course).

I appreciate all the help you give here at The Ranch

Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2280
    
  49
Well you still have a variable name starting with an upper case letter so on first inspection it looks odd.

BTW The question should be not "How does it look" but "Has it solved the problem", I take it you have compiled and run the code to see.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18563
    
    8

Tony Docherty wrote:Well you still have a variable name starting with an upper case letter so on first inspection it looks odd.


Especially as it's the same as the name of a class in the standard Java API: java.nio.file.Paths.
William Koch
Ranch Hand

Joined: Sep 26, 2012
Posts: 76
Yes, It still does look odd and I will begin fix it for future post. It did work! So thank you.

Especially as it's the same as the name of a class in the standard Java API: java.nio.file.Paths.


I do not believe that is currently affecting my program but yes I should not use the names of classes. I will work on these. Thanks a bunch.
Tony Docherty
Bartender

Joined: Aug 07, 2007
Posts: 2280
    
  49
I do not believe that is currently affecting my program

You are correct, but it is confusing to people who didn't write the code (and probably will be to you in a few months time).

Coding standards are there to help ensure everyone can easily understand the code you have written which is important especially when you asking others for help with your code.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception in thread "main" java.lang.NullPointerException