• Post Reply Bookmark Topic Watch Topic
  • New Topic

Classloader Tutorial throws errors  RSS feed

 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I used Mkyongs example but I get a null pointer - never used classloader before so I'm abit of a newbie with it! Does anyone know why? (using java 6) Test file is in same package as this java class.

 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't get two Null Pointer Exceptions (NPEs) at the same time; you must be getting it in line 17 and the the stack is unwinding to line 7 so both line numbers will appear on the same stack trace.
Assuming the NPE occurs at line 17: something on line 17 must be null.
It could be the class loader, the resource or the file from the getFile call. The class loader is hardly going to be null, but if you pass an incorrect resource name (here fileName) getResource tends to return null. If you actually find the resource as a URL it will probably be able to return its file name from the getFile method.
Try dividing line 17 into several lines with one dot operator in each and see whether that moves the Exception. Also check carefully that the test.txt file is actually in the location you think it is.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way: why have you got a Scanner field? You appear never to use it.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't use \n unless somebody tells you they want an LF character.
And if that inserts null repeatedly I have spelt the name of the property wrongly
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help again Campbell I normally use his tutorials to help me understand something new as they're quite good! I was looking it in reference to yesterday I got it suggested to me that I could use classloader to get the classes so I can access the contents of them? I could be barking up the wrong tree, but the way I was trying yesterday I was finding it hard to get it work and access the classes within the package so I thought i'd give this a shot and see if it can actually do what I've been told!

I thought it was line 17 but I just put it in as more info is more helpful at times - also I'll try your suggestion for line 17!
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain more about what accessing the contents of classes means. It might be something you would do with reflection.
Is this the tutorial you meant? You would appear to have misread the name of the source file. That will cause getResource to return null.
 
Alice Hampton
Ranch Hand
Posts: 217
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah that's the tutorial I meant : )


I just want to check if the class implements interface still but got suggested to try and use classloader today to see if that would work. Then I can put the classes that do in my list.
 
Campbell Ritchie
Marshal
Posts: 56533
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know whether the class loader solution will work or not, but you will probably get that to work when you provide the right file name. I think it is the relative path from the parent folder of your package structure, but I am not quite sure.

And yes, your explanation of the problem with line 17 was clear. A good explanation.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!