Java help.

Caporegime
Joined
12 Mar 2004
Posts
29,962
Location
England
Keeps saying it can't find the variable "phone1", but it's decleared right above it!?

Code:
import GSMNetwork.*;

public class send {	
	public static void main(String[] args) {
		try {
			MobilePhone phone1 = new MobilePhone("0123456789");
		} catch (NetworkNotAvailableException nna) {
			System.out.println("Network not available!");
			System.exit(1);
		}
		TextMessage txt = new TextMessage("Hi");
		phone1.sendMessage(txt, "9876543210");
	}
}
 
Code:
import GSMNetwork.*;

public class send {	
	public static void main(String[] args) {
		MobilePhone phone1;
		try {
			phone1 = new MobilePhone("0123456789");
		} catch (NetworkNotAvailableException nna) {
			System.out.println("Network not available!");
			System.exit(1);
		}
		TextMessage txt = new TextMessage("Hi");
		phone1.sendMessage(txt, "9876543210");
	}
}
 
I tried that earlier, but then I get, "phone1 might not have been initialised" and "unreported exception: phonenotinserviceexception, must be caught or declared to be thrown".
 
I tried that earlier, but then I get, "phone1 might not have been initialised" and "unreported exception: phonenotinserviceexception, must be caught or declared to be thrown".

Have you tried having it as an instance variable instead of a local variable

Or you could do as DJ_Jestar says but have it as MobilePhone phone1 = null; instead

As for the exception, not knowing anything about GSM API it's hard to tell, but I'm guessing phone1.sendMessage could throw a PhoneNotInServiceException
 
Last edited:
this might clear that error..
Code:
import GSMNetwork.*;

public class send {	
	public static void main(String[] args) {
		MobilePhone phone1;
		try {
			phone1 = new MobilePhone("0123456789");
		} catch (NetworkNotAvailableException nna) {
			System.out.println("Network not available!");
			System.exit(1);
			return;
		}
		TextMessage txt = new TextMessage("Hi");
		phone1.sendMessage(txt, "9876543210");
	}
}
 
Fixes the initialisation error :). The other error was caused the sendMessage method, fixed that now. :)
 
Last edited:
Back
Top Bottom