We help IT Professionals succeed at work.

Creating a two-dimensional Object array.

toymachiner62
on
1,929 Views
Last Modified: 2008-02-01
I'm trying to create a two-dimensional Object array so that way i can have the first two columns with strings in them and the third column with an int in it. heres the code i have so far. I always get the runtime error nullPointException at the line "parArray[i][j].setMine(mine1);"

************ObjArray Class**************
package pipelines;

public class ObjArray {

      public String name;
      public int dist;
      public Object[][] arr;

      
      public ObjArray()
      {
            name = null;
            dist = 0;
            arr = new Object[100][3];
      }
      
      public void setDist(int i)
      {
            dist = i;
      }
      
      public void setMine(String mineName)
      {
            name = mineName;
      }
      
      public int getDist()
      {
            return dist;
      }
      
      public String getMine()
      {
            return name;
      }
}

***************main class*****************

package pipelines;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.NoSuchElementException;
import java.util.Scanner;



public class MainClass
{
      public static  void main(String[] args) throws FileNotFoundException, NoSuchElementException
      {
            //reads the input file
            String filename = "blackgold.txt";
            Scanner scan = new Scanner(new File(filename));
            
            //creates the array
            ObjArray parArray[][] = new ObjArray[100][3];
            int i = 0;
            
            //while input file is not empty
            while(scan.hasNext())
            {
                  int j = 0;
                  
                  String mine1 = scan.next();
                  String mine2 = scan.next();
                  int dist = scan.nextInt();
                  
                  parArray[i][j].setMine(mine1);
                  parArray[i][j+1].setMine(mine2);
                  parArray[i][j+2].setDist(dist);
                  
                  i++;
            }
            
            scan.close();
      }
}
Comment
Watch Question

Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
you can achieve what u require more easily with  a sungle dimension array of beans where each bean stores your 2 string and an int

Commented:
as objects said you can define a been and use as your type.
here is full code;
public class MyObj {
 
      public String mine1;
      public String mine2;
      public int dist;
}
 
public class MainClass
{
      public static  void main(String[] args) throws FileNotFoundException, NoSuchElementException
      {
            //reads the input file
            String filename = "blackgold.txt";
            Scanner scan = new Scanner(new File(filename));
            
            //creates the array
            MyObj parArray[] = new MyObj[100];
            int i = 0;
            
            //while input file is not empty
            while(scan.hasNext())
            {
                  
                  String mine1 = scan.next();
                  String mine2 = scan.next();
                  int dist = scan.nextInt();
                  
                  parArray[i] = new MyObj();
                  
                  parArray[i].mine1 = mine1;
                  parArray[i].mine2 = mine2;
                  parArray[i].dist = dist;
                  
                  i++;
            }
            
            scan.close();
      }
}

Open in new window

Author

Commented:
For what i'm using it for i need to have it a two-dimensional array. So if i did it your way ysnky wouldnt that just overwrite the parArray[i]? like first it'd put mine1 in parArray[i] then it would overwrite it and put mine2 in there and then overwrite it and put dist in there. Or how does this work?

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.