Running this file returns the error:

Exception in thread "main" java.lang.NullPointerException
at Packet.<init>(test.java:20)
at test.main(test.java:46)


here:
line 20 : this.data[j] = data[j];
line 46 : Packet packet = new Packet(4,data,count,column);


can anyone help ma how to create this constructor that accepts String[][] ?

class Packet{
	
	int op_code;
	String[][] data;
	int column;
	int count;
	String type ;
	
	Packet(int op_code, String[][]data, int count, int column){
		
		data = new String[100][10];
        this.op_code = op_code;
        
        this.column = column;
        for(int i=0; i<count; i++)
        {
            for(int j=0; j<column; j++)
            {
                this.data[i][j] = data[i][j];
            }
        }
        type = "string[][]";
        this.count = count;
	}
	
}

public class test {
        

    public test() {
    }

    public static void main(String[] args) {
    	
    	int count = 3;
    	int column = 3;
    	
    	String[][] data = new String[3][3];
    	
    	data[0][0] = "00";
    	data[0][1] = "01";
    	data[1][1] = "11";
    	
        Packet packet = new Packet(4,data,count,column);
        
    }
}

actually the purpose of this constructor was to deliver a database resultset to client which is done else where and works ok with creating String[][] data. but when i use the Packet() constructor , i get a null exception.
thank you.

.

Sorry my bad ... after tweaking a bit got the solution...
As it turned out to be that constructors cant handle String[] properly .. so changed the code a bit

Packet(int op_code,  int count, int column){
	
	data = new String[100][10];
        this.op_code = op_code;	
        this.column = column;
        this.count = count;
	}
	
	public void setString(String[][] data){
		
	for(int i=0; i<count; i++)
        {
            for(int j=0; j<column; j++)
            {
                this.data[i][j] = data[i][j];
            }
        }
    
	}

... constructors cant handle String[] properly ...

That's news to me! Can you explain how you found that out?
Thanks

Maybe the problem was that in your first version you overwrote the data array parameter by coding

data = new String[100][10];

when it should have been

this.data = new String[100][10];

?
I really don't think there are any special restrictions or problems with String[][] in constructors

Constructors can handle String[] just fine, but perhaps constructor writers cannot.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.