DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 6 of 6
  1. #1
    harrytuttle89 Guest

    Help with arrays and null pointer assignments!!


    Could someone please be kind enough to tell me what I am doing wrong here.
    I have been racking my brain for a few days now and I just don't understand
    where the problem comes from.

    Bassically I have a class that I'm using to keep some static variables in.
    The constuctor gets called and the variables are initialized in the right
    way. However, when I recall them with my getter, it's as if the get methode
    has lost all track of them(the variables), and I get a null pointer assignment
    error. Why?

    Here Is my storage class:

    import javax.swing.JOptionPane;
    public class ColumnValues
    {
    private static final int MAX_COLS = 4; //
    private static int colValue[];
    private static int maxValue;

    public ColumnValues()
    {
    int colValue[] = new int[MAX_COLS];
    colValue[0] = 10;
    colValue[1] = 20;
    colValue[2] = 30;
    colValue[3] = 40;
    maxValue = 40;
    } // default constructor

    public static void setColValue(int colNum, int value)
    {
    colValue[colNum] = value;
    maxValue = ( colValue[colNum] >maxValue ? value : maxValue) ;
    } // end of setter

    public static int getColValue(int colNum)
    {
    return colValue[colNum];
    }
    public static int getMaxValue()
    {
    return maxValue;
    }

    } // end of class columnValues;

    Here is the driver:


    public class BarDriver
    {
    public static void main(String[] args)
    {
    ColumnValues cValues = new ColumnValues();

    int width, height;
    int dummyValue;
    dummyValue = ColumnValues.getColValue(1);
    JOptionPane.showMessageDialog(null, Integer.toString(dummyValue), "Hi",
    JOptionPane.INFORMATION_MESSAGE);

    + ...
    }

    : The second to the last line gives me null pointer error..

    Can some one please help and tell me what I did wrong? Thank you very much..



  2. #2
    Paul Clapham Guest

    Re: Help with arrays and null pointer assignments!!


    harrytuttle89 <harrytuttle89@hotmail.com> wrote in message
    news:3912405c$1@news.devx.com...
    >
    >
    > Here Is my storage class:
    >
    > public class ColumnValues
    > {
    > private static final int MAX_COLS = 4; //
    > private static int colValue[];
    > private static int maxValue;
    >
    > public ColumnValues()
    > {
    > int colValue[] = new int[MAX_COLS];
    > colValue[0] = 10;
    > colValue[1] = 20;
    > colValue[2] = 30;
    > colValue[3] = 40;
    > maxValue = 40;
    > } // default constructor
    >
    > public static void setColValue(int colNum, int value)
    > {
    > colValue[colNum] = value;
    > maxValue = ( colValue[colNum] >maxValue ? value : maxValue) ;
    > } // end of setter
    >
    > public static int getColValue(int colNum)
    > {
    > return colValue[colNum];
    > }
    > public static int getMaxValue()
    > {
    > return maxValue;
    > }
    >
    > } // end of class columnValues;
    >
    > Here is the driver:
    >
    >
    > public class BarDriver
    > {
    > public static void main(String[] args)
    > {
    > ColumnValues cValues = new ColumnValues();
    >
    > int width, height;
    > int dummyValue;
    > dummyValue = ColumnValues.getColValue(1);
    > JOptionPane.showMessageDialog(null, Integer.toString(dummyValue), "Hi",
    > JOptionPane.INFORMATION_MESSAGE);
    >
    > + ...
    > }
    >
    > : The second to the last line gives me null pointer error..
    >
    > Can some one please help and tell me what I did wrong? Thank you very

    much..
    >

    Don't make the getColValue() method and the colValue[] and maxValue fields
    static. As it is, your code doesn't initialize them (you don't have a
    static initializer). Static methods and fields don't belong to individual
    objects of the class. Then use

    dummyValue = cValues.getColValue(1);





  3. #3
    mike m Guest

    Re: Help with arrays and null pointer assignments!!


    Your using the class name and not the actual object.

    ColumnValues cValues = new ColumnValues();

    int dummyValue;
    dummyValue = ColumnValues.getColValue(1); //not an object

    use
    dummyValue = cValues.getColValue(1);

    i don't know if this was a type-o here or that this is your actual code...should
    w/ these changes.

    cheers,
    mike

    "harrytuttle89" <harrytuttle89@hotmail.com> wrote:
    >
    >Could someone please be kind enough to tell me what I am doing wrong here.
    > I have been racking my brain for a few days now and I just don't understand
    >where the problem comes from.
    >
    >Bassically I have a class that I'm using to keep some static variables in.
    >The constuctor gets called and the variables are initialized in the right
    >way. However, when I recall them with my getter, it's as if the get methode
    >has lost all track of them(the variables), and I get a null pointer assignment
    >error. Why?
    >
    >Here Is my storage class:
    >
    >import javax.swing.JOptionPane;
    >public class ColumnValues
    >{
    > private static final int MAX_COLS = 4; //
    > private static int colValue[];
    > private static int maxValue;
    >
    > public ColumnValues()
    > {
    > int colValue[] = new int[MAX_COLS];
    > colValue[0] = 10;
    > colValue[1] = 20;
    > colValue[2] = 30;
    > colValue[3] = 40;
    > maxValue = 40;
    > } // default constructor
    >
    > public static void setColValue(int colNum, int value)
    > {
    > colValue[colNum] = value;
    > maxValue = ( colValue[colNum] >maxValue ? value : maxValue) ;
    > } // end of setter
    >
    > public static int getColValue(int colNum)
    > {
    > return colValue[colNum];
    > }
    > public static int getMaxValue()
    > {
    > return maxValue;
    > }
    >
    >} // end of class columnValues;
    >
    >Here is the driver:
    >
    >
    >public class BarDriver
    >{
    > public static void main(String[] args)
    > {
    > ColumnValues cValues = new ColumnValues();
    >
    > int width, height;
    > int dummyValue;
    > dummyValue = ColumnValues.getColValue(1);
    > JOptionPane.showMessageDialog(null, Integer.toString(dummyValue), "Hi",
    >JOptionPane.INFORMATION_MESSAGE);
    >
    >+ ...
    >}
    >
    >: The second to the last line gives me null pointer error..
    >
    >Can some one please help and tell me what I did wrong? Thank you very much..
    >
    >



  4. #4
    harrytuttle89 Guest

    Re: Help with arrays and null pointer assignments!!


    "harrytuttle89" <harrytuttle89@hotmail.com> wrote:
    >
    >Could someone please be kind enough to tell me what I am doing wrong here.
    >


    Hey it's me again. Thank you for all the responses. Sorry for takeing so
    long to respond to your responses. I figured out what I was doing wrong.
    I declared and did not initialize colValue right here --------------------------------------
    |
    |
    |
    >Here Is my storage class: |
    > |
    >import javax.swing.JOptionPane; |
    >public class ColumnValues |
    >{ |
    > private static final int MAX_COLS = 4; // |
    > private static int colValue[]; <<-------------


    >private static int maxValue;
    >
    > public ColumnValues()
    > {
    > int colValue[] = new int[MAX_COLS];<<---
    > colValue[0] = 10; |
    > colValue[1] = 20; |
    > colValue[2] = 30; |
    > colValue[3] = 40; |
    > maxValue = 40; |
    > } // default constructor |
    > |

    |
    |
    Howver I redeclared and initialized colValue right here
    in the constructor. However, this version of colValue only had
    scope through out the constructor.
    Then when I try to access the access the original colValue
    in the getter; that version of colValue has not been initialized and I get
    a pointer error. The solution of coarse is just to take away the int from
    the colValue in the constructor, so I would have
    colValue = new int[MAX_VALUE];

    Agian thank you for all the earnest responses.

  5. #5
    harrytuttle89 Guest

    Re: Help with arrays and null pointer assignments!!


    "mike m" <michaelm@saccapital.com> wrote:
    >
    >Your using the class name and not the actual object.
    >
    >ColumnValues cValues = new ColumnValues();
    >
    >int dummyValue;
    >dummyValue = ColumnValues.getColValue(1); //not an object
    >
    >use
    >dummyValue = cValues.getColValue(1);
    >
    >i don't know if this was a type-o here or that this is your actual code...should
    >w/ these changes.
    >
    >cheers,
    >mike
    >

    Thanks for the response. However, because getColValue() is static, it is
    appropiate to use ColumnValues.getColValue(1). getColValue has a life independent
    of any instance variables of the class. This is similar to useing Math.random().
    Random is static and does not need to refer to a specific instance of Math.
    Your:
    int dummyValue;
    >dummyValue = ColumnValues.getColValue(1); //not an object
    >

    will also work and should give the same answer.

    Thanks
    -Harry


  6. #6
    Ilja Preuß Guest

    Re: Help with arrays and null pointer assignments!!


    Fine that you found the error yourself... :-)

    Nevertheless I have a short note: With your class you still have to instanciate
    (sp?) one object to get your static array initialized - which is bad, isn't
    it?

    Fortunately, there is another approach: do your initialization in a static
    block; it should look like this:

    static {
    int colValue[] = new int[MAX_COLS];
    colValue[0] = 10;
    colValue[1] = 20;
    colValue[2] = 30;
    colValue[3] = 40;
    maxValue = 40;
    }

    It is like a static method that will automatically be called once directly
    after the class definition is loaded (think of it as a "static constructor").

    Hope, this helps...


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links