Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Mar 2011
    Location
    san antonio,TX
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile Am having a compile error "java.lang.nullpointerexception"

    The name of the class is matrix and it compiles but when executed in blueJ.
    A problem arises; the code snipped is stated here below where the getvalue method is found.


    PHP Code:

    import java
    .util.ArrayList;


    public class 
    TextLab03st
    {
        public static 
    void main(String args[]) 
        {
            
    System.out.println("\nTextLab03 80-POINT VERSION\n");
            
            
    Matrix m1 = new Matrix();
            
    m1.displayMatrix("Matrix m1 Default Display");
            
    System.out.println();
        
            
    Matrix m2 = new Matrix(3,5);
            
    m2.displayMatrix("Matrix m2 3 X 5 Display");
            
    System.out.println();
            
    int pos 0;
            
    int count 100;
            for (
    int r 0m2.getRows(); r++)
            {
                for (
    int c 0m2.getCols(); c++)
                {
                    
    m2.setValue(r,c,count,pos);
                    
    pos++;
                    
    count++;
                }
            }       
            
    m2.displayMatrix("Matrix m2 3 X 5 Consecutive Integers Display");
            
    System.out.println();
            
            
    Matrix m3 = new Matrix(3,3,100);                
            
    m3.displayMatrix("Matrix m3 3 X 3 Initialized to 100 Display");
            
    System.out.println();
        }

    class 
    Matrix
    {
        
        private 
    ArrayList list; // one-dimensional array stores matrix values
        
    private int listSize;   // total number of elements in the matrix
        
    private int numRows;    // number of rows in the matrix
        
    private int numCols;    // number of cols in the matrix            
        
        
    public Matrix()
        {
           
    ArrayList list = new ArrayList();
           
    listSize 0;
           
    numRows 0;
           
    numCols 0;
        }

        public 
    Matrix(int r,int c)
        {
            
    numRows r;
            
    numCols c;
            
        }

        public 
    Matrix(int r,int c ,int value)
        {
            
    numRows r;
            
    numCols c;
            
            for(
    int i=0i<=r;i++)
            {
                for(
    int p=0p<=cc++)
                {
                    
    value p*i;
                }
            }
        }

        public 
    int getRows()
        {
            return 
    numRows;
        }

        public 
    int getCols()
        {
            return 
    numCols;
        }

        public 
    int getSize()
        {
            
    int listSize = list.size();

            return 
    listSize;    
        }

        public 
    int getValue(int r,int c)
        {    
            
    int rW getRows();
            
    int cL getCols();
            
            
    int pstn = (r*cL) + c;
            return ((Integer)list.
    get(pstn)).intValue();
            
            
    //(list.get(pstn)).intvalue(); 
        
    }

        public 
    void setValue(int r,int c,int value,int pst
        {
            
    int rW getRows();
            
            list.
    add(pst,new Integer(value));
        }

        public 
    void displayMatrix(String str)
        {
            
    System.out.println(str);
            
             for(
    int x=0x<=getRows();x++)
            {
                for(
    int z=0z<=getCols(); z++)
                {
                    
    System.out.print(getValue(x,z) + " ");
                }
                
    System.out.println("");
            }
        }

    Last edited by barillitos; 03-06-2011 at 05:45 PM. Reason: solved

  • #2
    Regular Coder
    Join Date
    Jan 2009
    Location
    india
    Posts
    145
    Thanks
    0
    Thanked 5 Times in 5 Posts
    Can you paste line number from exception stacktrace

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by servlet View Post
    Can you paste line number from exception stacktrace
    I don't think that will be necessary:
    PHP Code:
        public Matrix(int r,int c)
        {
            
    numRows r;
            
    numCols c;
            
        } 
    You're array list does not exist in your second example. Attempting to call list.get will result in a nullpointerexception on your m2.displayMatrix call when you attempt to run getValue on a non-object list. Consider chaining your constructors to prevent this from happening.
    That matrix should be cast to a generic type as well and applied to your data list. With a matrix, I wouldn't use an array list at all; the scalar 2 dimensional array makes more sense for a matrix (unless you're talking about hundreds of thousands of sparse entries, where some trickery will need to be performed which usually involves a list of some sorts; does not apply to dense entries for which a matrix usually is).
    PHP Code:
    class Matrix<T>
    {
        private 
    T[][] list;
        private 
    int iRows;
        private 
    int iCols;

        public 
    Matrix()
        {
            
    this(00);
        }
        public 
    Matrix(int iRowsint iCols)
        {
            
    this(iRowsiColsnull);
        }
        public 
    Matrix(int iRowsint iColsT data)
        {
            
    this.iRows iRows;
            
    this.iCols iCols;
            
    this.list = (T[][])new Object[iRows][iCols];
            if (
    data != null)
            {
                for (
    int i 0iRows; ++i)
                {
                    for (
    int j 0iCols; ++j)
                    {
                        
    this.list[i][j] = data;
                    }
                }
            }
        }

    Then change the method signatures to match T instead and rewrite for the (much easier) array instead of the collection.

    This of course only works if you are either happy with only accepting objects and not primitives. If you wanted to do primitives you'd need to write an setValue overload for each type and wrap them into their corresponding datatype.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    barillitos (03-06-2011)

  • #4
    New to the CF scene
    Join Date
    Mar 2011
    Location
    san antonio,TX
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    thanks for the info
    Last edited by barillitos; 03-06-2011 at 05:47 PM. Reason: not neccesary


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •