Win a copy of Java 9 Revealed this week in the Features new in Java 9 forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Imitating a file system in Java and absolutely stumped by NullPointerException  RSS feed

 
Leon Tee
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm writing a program that vaguely imitates a file system in java and I'm having trouble with my methods and my test class

So this is my main class.

I wrote code to:
1)Open a File
2) Copy a File
3) write output to another file
4)Get Path information
5)Get File Size
6) Read Lines in the file (if it's a text file)


For Copying, writing output, getting path/file and reading lines i keep getting




For the first one Im able to select a file, why isn't is still reference the file I selected?









Test Class



any help?
 
Carey Brown
Bartender
Posts: 2606
40
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are catching the exception and then displaying the message. That's throwing a whole lot of info away including the calling sequence and line number of where the exception was thrown. If you dump out the stack trace it will make your debugging a lot easier.
 
Leon Tee
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:You are catching the exception and then displaying the message. That's throwing a whole lot of info away including the calling sequence and line number of where the exception was thrown. If you dump out the stack trace it will make your debugging a lot easier.


thanks

heres what i got

For copy files:
java.lang.NullPointerException
at File3.TestFile.main(BasicFile.java:222)

For write output

For get path information:
java.lang.NullPointerException
at File3.TestFile.main(BasicFile.java:228)

For file size:
java.lang.NullPointerException
at File3.TestFile.main(BasicFile.java:231)

For read lines:

java.lang.NullPointerException
at File3.TestFile.main(BasicFile.java:234)
 
Carey Brown
Bartender
Posts: 2606
40
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must not have copied the entire source code file when you made your original post as those line numbers don't exist, at least according to your post. Always post the complete source code file including imports so that the line numbers match up.

As a guess I'd say you have a variable 'f' which is initialized to null and only set when you choose menu option '1'. Therefore, every time you run the code you must select option '1' followed by selecting the next option, such as Copy.
 
Carey Brown
Bartender
Posts: 2606
40
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have:

You are calling the a static method BasicFile() and you also have a class named BasicFile, this is confusing as hell. You should rename your static method, but do you really want this static method at all, why don't you just do a new of your BasicFile class?

You've declared a number of different variables with the name 'f'. Don't do this. Use meaningful names and be sure that you don't have local variables of the same name that hide the class member variables.

Your copyFile() method should be declared static as it is not using any member fields such as 'f'. Your copyFile() method will create a new file but does not do any actual copying; it is not using 'sourceFile'.

In your getAttributes() method you have

You'll end up with both the local 'file' variable and the field 'f' being set to null. When you call file.getAbsolutePath() you'll get a NPE.
 
Carey Brown
Bartender
Posts: 2606
40
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've charged ahead and wrote all your methods before testing any one of them. This is bad practice. Do your development incrementally and at each step make sure it compiles and runs correctly before going on to the next step.

You've mixed user interface styles, in some cases you use Options and in other cases you use a Scanner. You should be consistent and use one or the other.
 
Leon Tee
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:You have:

You are calling the a static method BasicFile() and you also have a class named BasicFile, this is confusing as hell. You should rename your static method, but do you really want this static method at all, why don't you just do a new of your BasicFile class?

You've declared a number of different variables with the name 'f'. Don't do this. Use meaningful names and be sure that you don't have local variables of the same name that hide the class member variables.

Your copyFile() method should be declared static as it is not using any member fields such as 'f'. Your copyFile() method will create a new file but does not do any actual copying; it is not using 'sourceFile'.

In your getAttributes() method you have

You'll end up with both the local 'file' variable and the field 'f' being set to null. When you call file.getAbsolutePath() you'll get a NPE.

thank you very much for the help!
 
Piet Souris
Rancher
Posts: 1828
61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, these guys at that forum better be very good, if they want to match Carey's quality replies. (a cow for Carey is on its way, tomorrow)
 
Leon Tee
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:You've charged ahead and wrote all your methods before testing any one of them. This is bad practice. Do your development incrementally and at each step make sure it compiles and runs correctly before going on to the next step.

You've mixed user interface styles, in some cases you use Options and in other cases you use a Scanner. You should be consistent and use one or the other.


You helped me figure out a bunch of the issues, thanks


I am having trouble with my search method however



with the error code



So I looked and it seems to be conflicting with an earlier scanner in my main method (testfile.java:45)



Is there anyway to resolve the conflict?

I think it's expecting an int based on the first scanner but i am trying to use a string for the one further down in the methods
 
Carey Brown
Bartender
Posts: 2606
40
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll need two Scanners, one for the file and one for keyboard input.

Close your file Scanner when you are done with it.

DO NOT close your keyboard Scanner as that will also close System.in.
 
Campbell Ritchie
Sheriff
Posts: 54033
130
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agree with Carey. You only ever need one Scanner pointing to System.in, but you will have to synchronise access to it in a threaded environment. You shou‍ld therefore probably not create new Scanner objects inside the loop.

Only ever use
while (myScanner.hasNextLine()) ...
when you are reading from a file, socket, or similar. If you try that for System.in, you will find System.in implicitly always has a next line, so that would be an infinite loop. The same applies to hasNext().
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!