[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

NullPointerException .......

Posted on 2004-09-29
3
Medium Priority
?
195 Views
Last Modified: 2010-03-31
Hi experts,

   I got  a java.lang.NullPointerException at line 00 while I tried to set size for a JButton .... does any one know what i might have missed .... thanks.

--------------------
       :
       :
 getContentPane().setLayout(null);
        getContentPane().setSize(600,600) ;
        getContentPane().setLocation(0,0) ;
        getContentPane().add(myHead) ;
          // set cookies location
        JButton cookies[] = new JButton[10] ;
        for(int i=0; i< cookiesNum; i++){
              int cookies_x, cookies_y ;
             
              cookies_x = ((int)Math.floor(Math.random()*1000)) % 500 ;
              cookies_y = ((int)Math.floor(Math.random()*1000)) % 500 ;
              cookies[i].setSize(20,20) ;                                       // line 00
              cookies[i].setLocation(cookies_x,cookies_y) ;
             
              getContentPane().add(cookies[i]) ;
             
          }
0
Comment
Question by:meow00
  • 2
3 Comments
 
LVL 1

Expert Comment

by:Dave_Toland
ID: 12185912
the array is empty.  try somthing like..

JButton cookies[] = new JButton[10] ;
cookies[0] = new JButton("A Button");
cookies[1] = new JButton("B Button");
cookies[2] = new JButton("C Button");
cookies[3] = new JButton("D Button");

        for(int i=0; i< cookiesNum; i++){
              int cookies_x, cookies_y ;
             
              cookies_x = ((int)Math.floor(Math.random()*1000)) % 500 ;
              cookies_y = ((int)Math.floor(Math.random()*1000)) % 500 ;
              cookies[i].setSize(20,20) ;                                       // line 00
              cookies[i].setLocation(cookies_x,cookies_y) ;
             
              getContentPane().add(cookies[i]) ;
             
          }
0
 
LVL 1

Accepted Solution

by:
Dave_Toland earned 1000 total points
ID: 12185940
actually, if you're array is of size 10 you either need to fill it with objects, set empty cells to null or detect the fact that the cell is empty:

the array is empty.  try somthing like..

JButton cookies[] = new JButton[10] ;
cookies[0] = new JButton("A Button");
cookies[1] = new JButton("B Button");
cookies[2] = new JButton("C Button");
cookies[3] = new JButton("D Button");

   //use a dynamic parameter in the for loop
        for(int i=0; i< cookies.length; i++){
              int cookies_x, cookies_y ;
             
              if(cookies[i] != null){

                      cookies_x = ((int)Math.floor(Math.random()*1000)) % 500 ;
                      cookies_y = ((int)Math.floor(Math.random()*1000)) % 500 ;
                      cookies[i].setSize(20,20) ;                                       // line 00
                      cookies[i].setLocation(cookies_x,cookies_y) ;
             
                      getContentPane().add(cookies[i]) ;
              }
          }
0
 
LVL 3

Assisted Solution

by:CI-Ia0s
CI-Ia0s earned 200 total points
ID: 12186166
Except that if you the array to be set up for all 10 values use a for loop. ;)

In fact, you could even put the object creation in your existing loop:

        for(int i=0; i< cookies.length; i++){
              int cookies_x, cookies_y ;
             
              JButton cookies[i] = new JButton("Click me!"); //This is the new line...

              if(cookies[i] != null){

                      cookies_x = ((int)Math.floor(Math.random()*1000)) % 500 ;
                      cookies_y = ((int)Math.floor(Math.random()*1000)) % 500 ;
                      cookies[i].setSize(20,20) ;                                       // line 00
                      cookies[i].setLocation(cookies_x,cookies_y) ;
             
                      getContentPane().add(cookies[i]) ;
              }
          }
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses
Course of the Month18 days, 4 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question