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
    Regular Coder
    Join Date
    Mar 2006
    Location
    Nigeria
    Posts
    192
    Thanks
    0
    Thanked 0 Times in 0 Posts

    class fields accessibility issues

    Im writing a midp class dat gets a list of items from a RMS and stores them in a multidimensinal array. the problem is dat the class variables are not getting into the constructor...here is wat i mean......
    Code:
    import .....;
    public class nmStore{
    public String [][] itemsFromStore;
    public void nmStore(){
    //statement to get items
    itemsFromStore = {{1,2},{3,4}}//1,2,3,4 rep retrieved items
    }
    }
    I got some compiler errors about the line
    Code:
    itemsFromStore = {{1,2},{3,4}}
    wen i try running ds. there was no more error wen i changed d line to
    Code:
    public String[][] itemsFromStore = {{1,2},{3,4}}//1,2,3,4 rep retrieved items
    . it however seem that the compiler cant even see dat d class variables here
    Code:
    public String [][] itemsFromStore;
    exists. Wats wrong? I however cant stick to using d work around since there are other methods within d class that also needs to access/modify the class variable.

  • #2
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    Four things.

    1) You can only instantiate an Int / String array when you declare it. Meaning when you declare String[][] itemsFromStore, that's when you can do the {} stuff. No other time.

    2) You are using int's instead of Strings in a String double array. i.e. if you want them to be strings do . .

    Code:
     
    public String[][] itemsFromStore = {{"1","2"},{"3","4"}}
    3) You don't have a semicolon
    Code:
    public String[][] itemsFromStore = {{"1","2"},{"3","4"}};
    4) You CAN instantiate the variables outside of the constructor if you wanted to. That may be your best bet if you insist on instantiation of this kind. However this seems rather ill suited towards robustness.

    Please in the future post the exact error message you are getting. That way we can more easily debug it. I mean, these errors were easy to spot, but future errors may not be.
    "To iterate is human, to recurse divine." -L. Peter Deutsch

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Location
    Nigeria
    Posts
    192
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for the reply. 2, 3 are actually typo errors and not exactly the code im working on.
    But do u really mean
    Quote Originally Posted by Aradon View Post
    1) You can only instantiate an Int / String array when you declare it. Meaning when you declare String[][] itemsFromStore, that's when you can do the {} stuff. No other time.
    ?
    Even when i tried using Strings instead of String array, its still the same thing.
    Here:
    Code:
    //myStore.java
    import java.io.*;
    
    public class myStore
    {
    	public String defaultEarnings;
    
    	public void myStore(){
    		defaultEarings = "something";
    	}
    
    };
    
    //myCanvas.java
    import java.util.*;
    import javax.microedition.lcdui.*;
    
    public class myCanvas extends Canvas
    {
    	Font normal;
    	public String earnings;
    	public myStore st;
    
    	public void paint(Graphics g){
    		st = new myStore();
    		earnings = st.defaultEarnings;
    		g.setGrayScale(255);
    		g.fillRect(0, 0, getWidth(), getHeight());
    		normal = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
    		g.setGrayScale(0);
    		g.setFont(normal);
    		g.drawString(earnings, getWidth()/2, getHeight()/2-titles.getHeight(), Graphics.BASELINE|Graphics.RIGHT);
    	}
    };
    This gives a complain like "illegal start of expression" on the line
    Code:
    defaultEarnings = "something"
    but works when i changed the line to
    Code:
    String defaultEarnings = "something"
    . However, with the new change, access to defaultEarnings from other methods within the class still points to the instance field (not the one in the constructor).
    But when I changed the codes to this
    Code:
    //myStore.java
    import java.io.*;
    
    public class myStore
    {
    	public String defaultEarnings = "something";
    
    	public void myStore(){
    		//no need for u now
    		//defaultEarings = "something";
    		//other expressions...
    	}
    
    };
    myCanvas.java gives a complaint like "nullpointer exception" on the drawstring line. What exactly is wrong?

  • #4
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    If this is indeed your code as is, then there are a couple errors in it.

    Firstly you do not need to end a class with }; Just a } will suffice in ending the class.

    Second, the part you commented out:

    Code:
    //defaultEarings = "something";
    "defaultEarings" is misspelled. It should be "defaultEarnings" Fix that and it should fix your String issue.

    Third you'll have to be more descriptive on where and what you're talking about with the null pointer exception, if this is indeed the case after these are fixed.
    "To iterate is human, to recurse divine." -L. Peter Deutsch


  •  

    Posting Permissions

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