Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

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: 4744
117
  • 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: 60081
188
  • 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: 9219
    177
    • 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
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!