Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-06-2011, 12:44 AM   PM User | #1
barillitos
New to the CF scene

 
Join Date: Mar 2011
Location: san antonio,TX
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
barillitos is an unknown quantity at this point
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
barillitos is offline   Reply With Quote
Old 03-06-2011, 10:52 AM   PM User | #2
servlet
Regular Coder

 
Join Date: Jan 2009
Location: india
Posts: 145
Thanks: 0
Thanked 5 Times in 5 Posts
servlet is an unknown quantity at this point
Can you paste line number from exception stacktrace
servlet is offline   Reply With Quote
Old 03-06-2011, 01:37 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
barillitos (03-06-2011)
Old 03-06-2011, 04:39 PM   PM User | #4
barillitos
New to the CF scene

 
Join Date: Mar 2011
Location: san antonio,TX
Posts: 4
Thanks: 1
Thanked 0 Times in 0 Posts
barillitos is an unknown quantity at this point
thanks for the info

Last edited by barillitos; 03-06-2011 at 05:47 PM.. Reason: not neccesary
barillitos is offline   Reply With Quote
Reply

Bookmarks

Tags
arraylist, arrays, java, matrix

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:22 AM.


Advertisement
Log in to turn off these ads.