Click here to Skip to main content
15,917,702 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have coded this program

This is sparse matrix class

Java
public class MatrixTerm {
 public int row,col,value;   
}

class SparsMatrix {
  private int Row,Col,Term;
 public  MatrixTerm SmArray[];
  int count=0;
  
  
  public SparsMatrix(int r,int c,int t){
      Row=r;
      Col=c;
      Term=t;
       SmArray =new MatrixTerm [20];
        }
  
  
  
  public void ReadSparse(){
        Scanner input= new Scanner(System.in);
      System.out.println("lotfan satr va  soton matrix vorodi ra vared konid ");
      Row=input.nextInt();
      Col=input.nextInt();
        int[][] matrix = new int [Row][Col];    
               for (int i=0;i<row;++i){>
                   for(int j=0;j<col;++j){>
                  System.out.println("lotfan "+"("+(i+1)+","+(j+1)+")"+" ra vared konid");     
                   matrix[i][j]=input.nextInt();
                   }}
               
              
                for (int i=0;i<Row;++i){>
                   for(int j=0;j<Col;++j){>
                       if(matrix[i][j]!=0){
                 SmArray[i].row=i;/// <big>HERE SAY java.lang.NullPointerException</big>
                 SmArray[i].col=j;
                 SmArray[i].value=matrix[i][j];
                           ++count;
                       }
                       
                   }
               }
  }
               
      public void show(){
          for(int i=0;i<count;++i){>
              System.out.println(SmArray[i]);
          }
    
}
               
               
  }



when i test the program in line 36 it say java.lang.NullPointerException
plz help me
Posted
Updated 31-Oct-11 7:42am
v2

1 solution

Check the value of i with the debugger. The Row value is dynamic but the declaration of the SmArray is hardcoded 20. SmArray =new MatrixTerm [20];

First of all, SparsMatrix is never called so the array isn't initialized. Second, you might consider using a list instead of an array because a list will expand as needed and therefor more efficient.

Add these imports:
C#
import java.util.Arrays;
import java.util.List;
import java.util.Iterator;


Change the array declaration to a generic list:
List smArray<matrixterm> = new ArrayList<matrixterm>();</matrixterm></matrixterm>


Change the MatrixTerm to add a constructor:
Java
public class MatrixTerm {
  public int row,col,value;
 
  public MatrixTerm(int row, int col, int value) {
    this.row = row;
    this.col = col;
    this.value = value;
  }
}


Add values to smArray like this:
smArray.add(new MatrixTerm(row, col, value));


Good luck!
 
Share this answer
 
v3
Comments
DominoBoy 31-Oct-11 8:33am    
SRY
i am new to java
may you explain more?
thx
DominoBoy 31-Oct-11 13:35pm    
thx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900