5 Replies - 2422 Views - Last Post: 15 March 2012 - 07:53 AM Rate Topic: -----

#1 Examiner12  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 89
  • Joined: 02-February 12

Pass 2d array from main into JTable

Posted 14 March 2012 - 02:52 PM

Hello All,

I have two classes. One reads a text file into a two dimmensional array StringValues. Where I'm stuck is with passing the array into a method within a second class.

Class ReadFileContents works fine:

package data;

import java.io.File; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Scanner;  
public class ReadFileContents {      

public static String[][] main(String[][] args) throws IOException { 
	
	
        Scanner s = new Scanner(new File("C:\\INV\\TEST.txt")); 
		List<String[]> list = new ArrayList<String[]>();     
		while (s.hasNextLine()) {         
		String[] line = s.nextLine().split(" ");       
		list.add(new String[] { (line[0]),           
								(line[1]) });      
		}         
		int numberOfRows = list.size();      
		int numberOfColumns = 2;      
		String[][] StringValues = new String[numberOfRows][numberOfColumns];     
		for (int i = 0; i < numberOfRows; i++) {    
		StringValues[i] = list.get(i);        
		System.out.println(StringValues[i][0] + " " + StringValues[i][1]);   
		}   
		return StringValues;
	} 
} 





class table encounters null pointer exception in main method:
package data;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class table extends JFrame{
	
	JTable table;
	
	public table(String[][] StringValues) throws IOException{
		setLayout(new FlowLayout());
		
		String[] columnNames = {"ECN", "Customer", "Model", "Serial"};
		
		String[][] data =  StringValues;
		
		/*
		Object[][] data = {
				{"WES1584680", "NCL34IR", "828", "1603"},
				{"MW3123WZEG", "AMDCCA", "22990-40", "423"},
				{"ME3XZ74RJ5", "AMDCCA", "22990-60", "372"},
				{"AAAY8ZKNW6", "AM23CA", "23", "58241172"},
				{"ME3XZX16UF", "AMD787A", "23-00-6IG", "61229"}			
		};
		*/
		table = new JTable(data, columnNames);
		table.setPreferredScrollableViewportSize(new Dimension(500, 70));
		table.setFillsViewportHeight(true);		
		JScrollPane scroll = new JScrollPane(table);
		add(scroll);
	}
	
	public static void main(String args[]) throws IOException
	{
		table gui = new table(null);
		gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		gui.setSize(600,200);
		gui.setVisible(true);
		gui.setLocationRelativeTo(null);
		gui.setTitle("Inventory Table GUI");
		
	}
}





Am I initializing the array in the table class wrong? String[][] data = StringValues;

You can see in the commented out portion of table method where I had hard-coded a test dataset for my 2d array values.

Any help is appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Pass 2d array from main into JTable

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,305
  • Joined: 20-September 08

Re: Pass 2d array from main into JTable

Posted 14 March 2012 - 03:33 PM

Quote

class table encounters null pointer exception in main method:


http://technojeeves....free/118-errors
Was This Post Helpful? -1
  • +
  • -

#3 Examiner12  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 89
  • Joined: 02-February 12

Re: Pass 2d array from main into JTable

Posted 14 March 2012 - 04:18 PM

While I did say that 'any help is appreciated', I was hoping for a little more constructive assistance than a link to a site the requires registration to attempt to interpret an error. If that was a jab, forgive me, I'm a beginner. I'm seeking to understand the source of the error, printing test values from StringValues array to test, and am not asking for a spoon-feeding answer here. If anyone else would weigh in on passing a 2d array to another class I'd greatly appreciate it. My StringValues array appears to have the same format as defined by my columnNames array. Thank you.
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,305
  • Joined: 20-September 08

Re: Pass 2d array from main into JTable

Posted 14 March 2012 - 04:24 PM

Quote

a site the requires registration to attempt to interpret an error.


It doesn't. But you either couldn't be bothered reading, or acting on, the half dozen or so lines on that page
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8327
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Pass 2d array from main into JTable

Posted 14 March 2012 - 08:43 PM

Just pass your String[][] as constructor to your table class (actually not a good idea to name table a class that is a JFrame and hold a JTable)

You can modify your main() it table to do a new table(data) using your test data[][]
That will do for your main()

Then you can also call that constructor from your ReadFileContent class. Don't make your main() method to throw an Exception instead use a try/catch clause in your reads and return a String[0][0] if there is an error. So if there is en error (the file does not exist) at least you will simply display an empty JTable instead of terminationg on an error.

Or if you want a dirty trick you can always call ReadFileContent.main() to get your data. Pass it a dummy String[][] as parameter as you don't use it anyway so why having put in there. It adds confusion, it make your main() looks the static void main() entry point in most classes.

package data;

import java.io.File; 
import java.io.IOException; 
import java.util.ArrayList; 
import java.util.List; 
import java.util.Scanner;  
public class ReadFileContents {      

public static String[][] main() { 
	
	
		List<String[]> list = new ArrayList<String[]>();     
       try {
        Scanner s = new Scanner(new File("C:\\INV\\TEST.txt")); 
		while (s.hasNextLine()) {         
		String[] line = s.nextLine().split(" ");       
		list.add(line);           
		}
        }
        catch(Exception e) {
        }         
		int numberOfRows = list.size();      
		String[][] StringValues = new String[numberOfRows][];     
		for (int i = 0; i < numberOfRows; i++) {    
		StringValues[i] = list.get(i);        
		System.out.println(StringValues[i][0] + " " + StringValues[i][1]);   
		}   
		return StringValues;
	} 
} 


This post has been edited by pbl: 14 March 2012 - 08:50 PM

Was This Post Helpful? 0
  • +
  • -

#6 Examiner12  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 89
  • Joined: 02-February 12

Re: Pass 2d array from main into JTable

Posted 15 March 2012 - 07:53 AM

Appreciate your assistance pbl. I'll try to post complete errors in the future, g00se, I wasn't at work when I was following up on here and couldn't access my code.

"Just pass your String[][] as constructor to your table class ": I'm having trouble bulding a constructor for StringValues within ReadFileContents because I already do String[][] StringValues = new String[numberOfRows][numberOfColumns];

I did take your try/catch vice IOException suggestion. I also changed table to tableBuild.

Now when I run the application I get a empty gui window with no JTable inside.

table is set up to accept StringValues as a parameter: public table(String[][] StringValues).
1. ReadFileContents works and does return StringValues;
2. table is setup to accept StringValues

Here's where I am now:

package data;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;

public class table extends JFrame{
	
	JTable table;
	
	public void tableBuild(String[][] StringValues) throws IOException{
		setLayout(new FlowLayout());
		
		String[] columnNames = {"ECN", "Customer", "Model", "Serial"};
		
		String[][] data =  StringValues;
		
		//String[][] data = ReadFileContents.main();
		
		
		/*
		Object[][] data = {
				{"WES1584680", "NCLNIR", "828", "1603"},
				{"MW3Y06WZEG", "AMDCCA", "22990-40", "423"},
				{"ME3XZ74RJ5", "AMDCCA", "22990-60", "372"},
				{"ME3Y8ZKNW6", "AMDCCA", "23", "58241172"},
				{"ME3XZX16UF", "AMDCCA", "23-00-6IG", "61229"}			
		};
		*/
		
		table = new JTable(data, columnNames);
		table.setPreferredScrollableViewportSize(new Dimension(500, 70));
		table.setFillsViewportHeight(true);		
		JScrollPane scroll = new JScrollPane(table);
		add(scroll);
	}
	
	public static void main(String args[]) throws IOException
	{
		table gui = new table();
		gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		gui.setSize(600,200);
		gui.setVisible(true);
		gui.setLocationRelativeTo(null);
		gui.setTitle("Inventory Table GUI");
		
	}
}








package data;

import java.io.File; 
import java.io.FileNotFoundException;
import java.util.ArrayList; 
import java.util.List; 
import java.util.Scanner;  
public class ReadFileContents {      

public static String[][] main() { 	
	

	
        Scanner s = null;
		try {
			s = new Scanner(new File("C:\\INV\\TEST.txt"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} 
		List<String[]> list = new ArrayList<String[]>();     
		while (s.hasNextLine()) {         
		String[] line = s.nextLine().split(" ");       
		list.add(new String[] { (line[0]),           
								(line[1]) });      
		}         
		int numberOfRows = list.size();      
		int numberOfColumns = 2;      
		String[][] StringValues = new String[numberOfRows][numberOfColumns];     
		for (int i = 0; i < numberOfRows; i++) {    
		StringValues[i] = list.get(i);        
		System.out.println(StringValues[i][0] + " " + StringValues[i][1]);   
		}   
		return StringValues;
		
	} 
} 






Error: None, just an empty Gui with no JTable.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1