Solved

NullPointerException .......

Posted on 2004-09-29
3
190 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
windows explorer path to command prompt 5 54
factorial example 4 50
ejb on wildfly 5 30
Fast way to search item into Java Array (Rhino compatible) 2 31
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

820 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