This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java and have Dr. Raoul-Gabriel Urma & Richard Warburton on-line!
See this thread for details.
Win a copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Classloader Tutorial throws errors

 
Ranch Hand
Posts: 217
Eclipse IDE Chrome 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.

 
Marshal
Posts: 68036
258
  • 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: 68036
258
  • 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: 68036
258
  • 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
Eclipse IDE Chrome 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: 68036
258
  • 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
Eclipse IDE Chrome 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: 68036
258
  • 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.
 
He puts the "turd" in "saturday". Speaking of which, have you smelled this tiny ad?
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!