Results 1 to 9 of 9
  1. #1
    cenafu is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default problem in accessing array values of one class in to jframe class

    hello,
    i have a program named test3, which contains an array with values
    but i wan to access these or copy values in to other prog named test2
    the test2 is a jframe(swing) code
    while i do it .. shows null pointer exception...
    so plz help me

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Can you show your code, what you are trying to do? If you do post code, please read the forum FAQ regarding use of code tags. Best of luck!

  3. #3
    cenafu is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default here is the code,

    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.util.Random;
    
    
    class properties
    {
         int drop_rate;
         
    }    
         
         
        
    class neighbours extends properties
    {
        int id;
        //properties prop;
        int out_of_range;
        int ref;
        neighbours()
        {
         drop_rate=0;
        }
    }
    class node extends neighbours
    {
        int id;
        
        int range;
       
        int row,col;
        int battery;
    }
    
    class test3
    {
      int n_nodes=20;
     
      node table[]=new node[n_nodes];
      
         void createnodes(int no,int range)
         {
          int i,j,r,c;
          Random ran = new Random(); 
          
            
          
           for(i=0;i<no;)
             {
    	 //random positions                      //---------------------->/////
    	     table[i]=new node();
    	      r=ran.nextInt(490);
    	      c=ran.nextInt(490);
    	      if(duppos(c,r,i)!=1)   //find duplicate pos
    	       {
    	     //if position are not duplicate assign
    	        table[i].id=i+1;
    			table[i].row=r;
    			table[i].col=c;
    			table[i].battery=10;
    			table[i].range=range;
                  
    	   
    			i++;
    			}
    		}   //end of for
    
    	  } 
    public void printme()
    {
      for(int i=0;i<n_nodes;i++)
    {
      System.out.println("node "+i+" row "+table[i].row+" col "+table[i].col);
    System.out.println("");
    }
    }
        int duppos(int x,int y,int max)
         {
          int i,fl=0;
           for(i=0;i<max;i++)
           if(table[i].col==x && table[i].row==y)
    	  {
    	      fl=1;
    	      break;
    	  }
    
          return fl;
         }
    public void show()
    {
      test5 t5=new test5();
    }
    public void showme()
    {
     System.out.println("hello");
    }
       public static void main(String args[])
       {
        System.out.println("its fine");
             
    
    
        test3 t =new test3();
        t.createnodes(20,5);
        t.printme();
    t.show();
     
       System.out.println("its fine once again");
       }
    }
    Last edited by cenafu; 03-21-2009 at 10:23 AM.

  4. #4
    cenafu is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default

    Java Code:
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.util.Random;
    import java.util.*;
    public class test2 extends JFrame
    {
      	public test2()
      	 {
    		initComponents();
    	}
    	
    
    
    	//private JLabel[] jLabel1;
    	Random r = new Random();
    
                 JFrame frame=new JFrame("Manet");
    //------------------------------------------------>>>>>>>>
       test3 t=new test3();
    
    	  private void initComponents() 
    	  {
    	    GridBagConstraints gridBagConstraints;
    
    		JButton[] jLabel1=new JButton[20];
    		
    		
    		//JLabel[] jLabel1 = new JLabel[10];
    		
    	   
    	   frame.getContentPane().setLayout(new GridBagLayout());
    		//setTitle("MANET");
    		frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    	
    	    for(int i=0;i<20;i++)
    	    {
    	   jLabel1[i]=new JButton();
    		jLabel1[i].setText("node"+i);
    		    
    		
    		gridBagConstraints = new GridBagConstraints();
                
    //------------------------------------------------------------>>>>>>>>>>>
       
    		//gridBagConstraints.gridx = r.nextInt(600);
    		//gridBagConstraints.gridy = r.nextInt(300);
    //---->>     
              gridBagConstraints.gridx =t.table[i].row;
    		gridBagConstraints.gridy = t.table[i].col;
    
    		//gridBagConstraints.gridwidth =r.nextInt(100);
    		//gridBagConstraints.ipadx =r.nextInt(100);
    		//gridBagConstraints.ipady =r.nextInt(100);
    		//gridBagConstraints.insets = new Insets(10, 10, 10,10);
    
    		frame.getContentPane().add(jLabel1[i], gridBagConstraints);
           
      }
            try{
                  Thread.sleep(400);}
                catch(Exception e)
               {System.out.println("not working");}
             frame.setSize(800,500);
     pack();
    		frame.setVisible(true);
                 
       }        
    
       
    
    		
    		
    		
    	   }
    	   
    	   public static void main(String args[])
    	    {
    		new test2();
    		}
    		
    		
    }
    Last edited by cenafu; 03-21-2009 at 10:25 AM.

  5. #5
    cenafu is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default

    the first code creates random numbers and stores in table array,
    (row,column)

    i need this array with values in my second program to position the buttons
    i did it by creating an instance of first class in the second class
    but not able to access values in table array... it shows NullPointerException


    please help... am new to java

  6. #6
    cenafu is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default

    i have indicated problem through arrow mark------->>>

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Your table nodes are all null. do you ever call createNodes on your table?

  8. #8
    cenafu is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

    Default

    I compile both the codes at a time,
    so in the first program random numbers are generated
    and are stored in table array

    this array i need in second program to put values in
    test3 t=new test3();

    gridBagConstraints.gridx =t.table[i].row;
    gridBagConstraints.gridy = t.table[i].col;

    But not possible jus by instantiating ... do tel me wat shall i do

    plz help

  9. #9
    cenafu is offline Member
    Join Date
    Mar 2009
    Posts
    7
    Rep Power
    0

Similar Threads

  1. Accessing array in other class
    By ce3c in forum New To Java
    Replies: 8
    Last Post: 02-23-2009, 12:07 AM
  2. writing array values to another class
    By ronald christian in forum New To Java
    Replies: 27
    Last Post: 11-07-2008, 05:08 PM
  3. accessing return values from another class
    By moaxjlou in forum New To Java
    Replies: 3
    Last Post: 10-31-2008, 03:25 AM
  4. Replies: 4
    Last Post: 04-15-2008, 02:04 PM
  5. Replies: 0
    Last Post: 02-17-2008, 10:03 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •