Solved

NullPointerException .......

Posted on 2004-09-29
3
188 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 250 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 50 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

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
printing a file in reverse order is easy in recursion rather than in iteration 3 56
eclipse java  build path 6 57
servlet example 17 32
What's wrong with this code? 4 23
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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:

772 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