-1

I'm really not sure why this is happening. It seems as if getLine1Number isn't being instantiated - but it seems as if the 2nd reference to it doesn't need it (it throws no errors when I comment out the null check.

WORKING:

public class StartActivity extends Activity implements OnClickListener {

    Button goButton;
    Context c;
    boolean isAirPlaneMode, isMDNPresent = false;//boolean values to check for airplane mode and if the sim populates the MDN
    int simState;
    TelephonyManager tm;
    boolean NetworkConnection = false;//boolean to check the Network Availability
    AlertDialog mConfirmAlert = null;
    TextView text;
    TextView mUpdatetext;
    int version;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.start);
        version = android.os.Build.VERSION.SDK_INT;
        tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        // to read the SIM state
        simState = tm.getSimState();
        System.out.println("Sim State" + simState);
        //if (tm.getLine1Number = null) {
            //isMDNPresent = true;
        //}
        // to check for MDN
        if (tm.getLine1Number().equalsIgnoreCase("")) {
            isMDNPresent = true;
        }

THROWS ERROR: getLine1Number cannot be resolved or is not a field

public class StartActivity extends Activity implements OnClickListener {

    Button goButton;
    Context c;
    boolean isAirPlaneMode, isMDNPresent = false;//boolean values to check for airplane mode and if the sim populates the MDN
    int simState;
    TelephonyManager tm;
    boolean NetworkConnection = false;//boolean to check the Network Availability
    AlertDialog mConfirmAlert = null;
    TextView text;
    TextView mUpdatetext;
    int version;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.start);
        version = android.os.Build.VERSION.SDK_INT;
        tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        // to read the SIM state
        simState = tm.getSimState();
        System.out.println("Sim State" + simState);
        if (tm.getLine1Number = null) {
            isMDNPresent = true;
        }
        // to check for MDN
        if (tm.getLine1Number().equalsIgnoreCase("")) {
            isMDNPresent = true;
        }
HelloMojo
  • 367
  • 1
  • 4
  • 20

3 Answers3

0

it should be

    if (tm.getLine1Number() == null) {

note the ()

Shubhank
  • 21,721
  • 8
  • 65
  • 83
0

In the working part it is a method

if (tm.getLine1Number().

In the non-working code it is used as a variable (no"()")

if (tm.getLine1Number = null)

Also you want to compare not initialize so change it from

if (tm.getLine1Number = null)

to

if (tm.getLine1Number() == null)

Add the extra "="

codeMagic
  • 44,549
  • 13
  • 77
  • 93
  • Ok. (Thanks - that should have been pretty obvious for me.) My only problem is when I use: if (tm.getLine1Number == null) { isMDNPresent = true; } I'm getting: "getLine1Number cannot be resolved or is not a field" – HelloMojo Oct 11 '13 at 17:24
  • Yes, sorry, I copied your code and didn't add the "()" back in. I have edited. – codeMagic Oct 11 '13 at 17:25
0

In the first place you wrote 'tm.getLine1Number = null' as if it is a field. In the second place you wrote 'tm.getLine1Number().equalsIgnoreCase("") as if it is a method. That is why one work and one does not.

trungdinhtrong
  • 2,604
  • 2
  • 17
  • 26