Errors running Java program

后端 未结 1 1774
长发绾君心
长发绾君心 2021-01-29 16:17

Im tying to run my java program using netbeans and im getting this error, any suggestion?

Exception in thread \"AWT-EventQueue-0\" java.lang.NullPointerException         


        
1条回答
  •  -上瘾入骨i
    2021-01-29 17:04

    It looks like one (or more) of the arguments you are passing into your ImageIcon constructor are null.

    EDIT

    The exception is occurring at one of these two lines:

    ImageIcon image = new ImageIcon(MainForm.class.getResource("SHA_logo.gif"));    
    

    or

    image = new ImageIcon(MainForm.class.getResource("maryland_flag_round.gif")); 
    

    In either case, the problem is the same: the resource is not being found, and so null is being returned by getResource(). Why aren't you just using new ImageIcon(String filename)? I'm not even 100% sure what getResource() is doing.

    A few other quick comments (suggestions) on your code:

    pane.setLayout(null); is not a good idea, I'm not even sure you can just get rid of the LayoutManager like that.

    Reusing the same reference (image and label) multiple times for different objects is bad coding style. You're not saving any memory by doing that, and the code makes less sense.

    Use more descriptive names! button1 should probably be called 'interchangeButton' or something along those lines. For such a small segment of code, no one will get lost, but if you ever work on a larger project, a good naming scheme is vital.

    0 讨论(0)
提交回复
热议问题