This is not running in Eclipse but ran in other jvm. Not sure if there is anything missing :(

import javax.swing.JOptionPane;
import java.io.*;

public class file1 {
static int[][] number=new int[1000][1000];;
static int row,column;

    public static void main(String[] args) {

   readFile();
   /*for(int i=0;i<row;i++)
   	for(int j=0;j<column;j++)
   		System.out.println(number[i][j]);
		*/
	String result="";

	for(int i=0;i<row;i++)
		result+="Row "+(i+1)+" Minimum value: "+findMinimum(i)+"\n";
	JOptionPane.showMessageDialog(null, result);

	JOptionPane.showMessageDialog(null, "Even numbers in array are: "+displayEven());

	JOptionPane.showMessageDialog(null, "Sum of all number is: "+getSum());
	//prints the box with dialogue minimum vale, even numbers in array 
	//and sum of all number is:  
for(int i=0;i<row;i++)
	sortRow(i);

JOptionPane.showMessageDialog(null, "After sorting each row:\n"+Print());
    }
    
    // prints the box with dialoge after sorting each row
    



private static void readFile(){

	String ver,hor,temp;
	String[] copy=new String[1000];
	int i=0,j,len=1,beg=0,end=0;

	
	try{
	FileReader fr = new FileReader("test.txt");
	BufferedReader br = new BufferedReader(fr);


		while((copy[i++] = br.readLine()) != null);
			
			fr.close();		
			i--;	
	}catch(IOException e){}
		while(copy[0].charAt(end)!=',')
			end++;
			
		ver=copy[0].substring(beg,end);
		row=Integer.parseInt(ver);
		
		beg=end;
		while(end<copy[0].length())
			end++;
		
		hor=copy[0].substring(beg+1,end);
		column=Integer.parseInt(hor);
				
		
		i=0; j=0;		
		while(i<row){
			end=0;beg=0;j=0;
			while(end<copy[i+1].length())
			{
				try{
						while(copy[i+1].charAt(end)!=','  &&  end<copy[i+1].length())
							end++;
						
						}catch(StringIndexOutOfBoundsException e){}
						
					temp=copy[i+1].substring(beg,end);
				
					number[i][j]=Integer.parseInt(temp);
					
					System.out.println(number[i][j]);
					beg=++end;
					j++;
				}
				//JOptionPane.showMessageDialog(null,"The sum is"+integers[i],"Samir",JOptionPane.WARNING_MESSAGE);	
			i++;
			}


			
}






private static int findMinimum(int row)
{
int min=0,max=0;
            for(int i=0;i<column;i++)
              if(number[row][i]>max)
                  max=number[row][i];
min=max;
            for(int i=0;i<column;i++)
                if(number[row][i]<min)
                    min=number[row][i];

return min;
}






private static String displayEven()
{
String result="";

for(int i=0;i<row;i++)
    for(int j=0;j<column;j++)
        if(number[i][j]%2==0)
            result+=number[i][j]+" ";

return result;
}






private static int getSum()
{
int result=0;

for(int i=0;i<row;i++)
    for(int j=0;j<column;j++)
        result+=number[i][j];
return result;
}






private static void sortRow(int inrow)
{
int tempo;
    for(int j=0;j<column;j++)
    for(int i=1;i<column;i++)
        if(number[inrow][i]<number[inrow][i-1])
        {
        tempo=number[inrow][i];
        number[inrow][i]=number[inrow][i-1];
        number[inrow][i-1]=tempo;
        }
}





private static String Print()
{
String result="";

for(int i=0;i<row;i++){
    for(int j=0;j<column;j++)
        result+=number[i][j]+" ";
	result+="\n";
	}

return result;
}



}

Dani AI

Generated

Quick diagnosis: the NullPointerException in readFile means the code is trying to use a String that is null (most likely copy[0]) because the file read never produced lines. Two common causes in Eclipse are (a) the program could not open "test.txt" and the IOException was swallowed, leaving copy full of nulls, or (b) the code later assumes lines exist without checking bounds. was right that the stray semicolon is worth noticing, but that double-semicolon is harmless. steered you toward a null check — that idea is on the right track.

Concrete checks to run now:

  • Print the working directory to confirm where Eclipse is looking for files: System.getProperty("user.dir").
  • Print the absolute path of "test.txt" and verify the file exists in that folder (or use an absolute path).
  • Stop swallowing exceptions; at minimum call e.printStackTrace() in the catch so you see a FileNotFoundException if the file is missing.

Safer reading/parsing approach (use this instead of filling a fixed array blindly):

  • Read all lines into a List<String> with a try-with-resources BufferedReader (or Files.readAllLines), handle IOException, then validate lines.size() before accessing lines.get(0).
  • Parse the first line with split(",") and trim() to get rows and columns, and validate the parsed integers.
  • For each data line, split on comma, check the number of tokens, and handle NumberFormatException if parsing fails.

Extra cautions: avoid indexing copy[i+1] without ensuring i+1 < lines.size(), catch and log NumberFormatException around Integer.parseInt, and keep parsing defensive (report malformed input). Once the file-reading is robust, the NullPointerException will disappear and the rest of the logic can be debugged safely.

Recommended Answers

All 4 Replies

The problem may be the 2 semicolons in line 5.

if(copy[0]==null) System.out.println("DEBUGGING IS IMPORTANT");

Put that in your readFile method before the while loop.

line number please

if(copy[0]==null) System.out.println("DEBUGGING IS IMPORTANT");

Put that in your readFile method before the while loop.

after running i am getting this error messages:
Exception in thread "main" java.lang.NullPointerException
at file1.readFile(file1.java:55) **here it should be 57
at file1.main(file1.java:10)

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.