i am trying to accept a 3 by 4 integer matrix from the user and an unable to do so.here is my code.

void accept()throws IOException
	{
		int i,j;
		InputStreamReader instream = new InputStreamReader(System.in);
		BufferedReader stdin = new BufferedReader(instream);
		System.out.println("\nEnter the coefficients and the constants for the 3 equations  ");
		for(i=0;i<3;i++)
		{
			for(j=0;j<4;j++)
			{
				System.out.println("M["+i+"]["+j+"]=");
				mat1[i][j]=Integer.parseInt(stdin.readLine());
			}
		}	
	}

Thanks in advance!please help

Recommended Answers

All 9 Replies

what is your error?

the error when irun the program::

Enter the coefficients and the constants for the 3 equations
M[0][0]=
2
value is2
Exception in thread "main" java.lang.NullPointerException
at mypack.matrix.accept(classfile.java:21)
at mypack.Linear_Equation.main(Linear_Equation.java:8)

I cannot see at what point you have put a System.out.println() for "value is2". Also show us where and how the method "accept()" is being called and how the matrix is being initialised.

Also I guess it would be nice if you would "close()" your BufferedReader, once you are done using it in that method.
The developers did not give the BufferedReader class a "close()" method just for displaying in the javadocs.

"value is2" where does this come from?

your code above has nothing about this...

"value is2" where does this come from?

your code above has nothing about this...

there are 2 file:
Linear_Equation.java is one
and the other si classfile.java both are in mypack.

LinearEquation.java:

package mypack;

import java.io.*;

public class Linear_Equation{
    public static void main(String args[])throws IOException{
        matrix mat1=new matrix();
        mat1.accept();
        mat1.display();

    }


}

classfile.java:

package mypack;

import java.io.*;

class matrix
{
    int mat1[][];
    void accept()throws IOException
    {
        int i,j,ch;
        InputStreamReader instream = new InputStreamReader(System.in);
        BufferedReader stdin = new BufferedReader(instream);
        System.out.println("\nEnter the coefficients and the constants for the 3 equations  ");
        for(i=0;i<3;i++)
        {
            for(j=0;j<4;j++)
            {
                System.out.println("M["+i+"]["+j+"]=");
                ch=Integer.parseInt(stdin.readLine());
                System.out.println("value is"+ch);
                mat1[i][j]=ch;
                System.out.println("value is"+mat1[i][j]);
            }
        }   
    }
    void display()
    {
        int i,j;
        System.out.println("\nThe matrix is:\n");
        for(i=0;i<3;i++)
        {
            System.out.println("\n");
            for(j=0;j<3;j++)
                System.out.println(""+ mat1[i][j]);
        }   
    }
    void echolonform()
    {

    }

}

and the error is:

Enter the coefficients and the constants for the 3 equations  
M[0][0]=
2
value is2
Exception in thread "main" java.lang.NullPointerException
    at mypack.matrix.accept(classfile.java:21)
    at mypack.Linear_Equation.main(Linear_Equation.java:8)

Edit your post, the code tags go in square brackets ([) not angled brackets, like here:-

[code=java] // Your code goes here

[/code]

should use [] for code tags...

anyway, it's hard to read but i think need to create a proper constructor for matrix. you cannot use an array like a vector.

According to me this is what is causing the problem :-

int mat1[][];

You have never initialized your matrix, with any size.
You will have to initialize your matrix as follows to be able to use it:-

int[][] mat1 = new int[3][4];

Heres a tutorial to help you on arrays.

thanks!!this is working jus fine!!

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.