• Post Reply Bookmark Topic Watch Topic
  • New Topic

Application crashes when it is run  RSS feed

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hey guys so this is my code , it compiles correctly but when i try to run the application , i get this error message : Exception in thread "main" java.lang.NullPointerException
        at Stock.closeFile(Stock.java:105)
        at Stock.<init>(Stock.java:71)
        at Stock.main(Stock.java:35)

I spent a few hours trying to fix it but i'm completely stumped. Any help will be greatly appreciated
 
Saloon Keeper
Posts: 4413
108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
closeFile should catch all exceptions, not just IOException. NullPointerException is not a subclass of IOException.
 
Marshal
Posts: 58823
179
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't make subclasses of JFrame or similar; it is hardly ever necessary to override its methods. Overriding JPanel#paintComponent() is probably the most common overriding done.

The line numbers don't seem to match the exception, but it is most likely that input is null. I can see all sorts of problems about input.
  • 1: Don't declare such readers or input streams as fields. They should be local variables with a limited scope.
  • 2: Always close everything reading a file or writing to it, whether or not an exception is thrown. The close() call shouldn't be in the catch(). That way the resource is closed immediately after use.
  • 3: The best way to close a reader is try‑with‑resources, but that is only available in versions of Java® written since Noah's Ark.
  • I think what is happening is that you are closing the display without reading the file, at which point the input hasn't been initialised because you didn't execute line 59.
    This is what comes from trying to write GUI code and then attach business logic to it. If you had written the business logic first you would have opened, read completely, and closed the file all in one method. You would have some sort of collection of stock objects with name and price. Then you could add the GUI and use the button to update the display.
    I presume from your using a data input stream that the file is a binary file. For text files use a file reader and a buffered reader. You can read more about that in the Java™ Tutorials.
     
    Yogi Permaul
    Greenhorn
    Posts: 26
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    i'm so confused as to how to go about fixing this , i dont even know where to start.
    I relocated the file to the correct directory and got it to run correctly but i cannot submit the application with my directory. What is the simplest way to get my App to work?
     
    Saloon Keeper
    Posts: 8724
    162
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The problem is that when you open the file, an exception is thrown (probably because the file wasn't found), but then you catch the exception and continue with closeFile(), even though input is still set to null.

    Why would you want to close the file if you didn't manage to open it in the first place?

    Anyway, you need to start improving your code style, as we have mentioned in other topics. Your code will be easier to debug if you follow the tips we have given you.
     
    Yogi Permaul
    Greenhorn
    Posts: 26
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Stephan van Hulst wrote:The problem is that when you open the file, an exception is thrown (probably because the file wasn't found), but then you catch the exception and continue with closeFile(), even though input is still set to null.

    Why would you want to close the file if you didn't manage to open it in the first place?

    Anyway, you need to start improving your code style, as we have mentioned in other topics. Your code will be easier to debug if you follow the tips we have given you.



    Ah thank you after reading your explanation it took me 5 minutes to identify and fix the issue. My app is now running perfectly. Sorry about the coding style , i'm taking an intro course into Java and using a textbook that is ten years old
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!