Search code examples
javaandroidtelephonymanager

Null check results in getLine1Number cannot be resolved or is not a field


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;
        }

Solution

  • 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 "="