View Full Version : class fields accessibility issues

11-03-2007, 07:07 PM
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......

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
itemsFromStore = {{1,2},{3,4}}
wen i try running ds. there was no more error wen i changed d line to
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
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.

11-03-2007, 08:59 PM
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 . .

public String[][] itemsFromStore = {{"1","2"},{"3","4"}}

3) You don't have a semicolon

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.

11-06-2007, 07:56 AM
thanks for the reply. 2, 3 are actually typo errors and not exactly the code im working on.
But do u really mean

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.

import java.io.*;

public class myStore
public String defaultEarnings;

public void myStore(){
defaultEarings = "something";


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.fillRect(0, 0, getWidth(), getHeight());
normal = Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
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
defaultEarnings = "something" but works when i changed the line to
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

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?

11-06-2007, 02:08 PM
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:

//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.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum