Why does my icon handling code throw a NullPointerException?

后端 未结 6 2231
梦如初夏
梦如初夏 2020-12-16 07:12

I have added an image for my button,but when I run that frame this exception will be thrown .why?please help me.

init:

deps-jar:
compile-single:
run-single:         


        
6条回答
  •  离开以前
    2020-12-16 07:40

    This means, that getClass().getResource("/Images/yahoo_1.gif") returns null.

    JavaDoc states that this happens if

    the resource could not be found or the invoker doesn't have adequate privileges to get the resource.

    1. Check if getResource really returns null:
      System.out.println(getClass().getResource("/Images/yahoo_1.gif"));

    2. Make sure that your path is correct and that it is in your classpath.

    EDIT:

    I just tried it with NetBeans. I created the following structure

    Source Packages
      Images
        yahoo_1.gif
    

    and your code worked fine. Is this your structure?

    Try to right-click on your application and select Clean and Build.

提交回复
热议问题