Exception in thread "main" java.lang.NullPointerException Problem!!
hi,
I am new in java programming
i run this program and get error message
(Exception in thread "main" java.lang.NullPointerException at CheckAge.main(CheckAge.java:12)
here is my full coding:
import java.util.Scanner;
class CheckAgeForDiscount {
public static void main(String args[]) {
Scanner myScanner = new Scanner(System.in);
int age;
double price = 0.00;
char reply;
System.out.print("How old are you? ");
age = myScanner.nextInt();
System.out.print("Have a coupon? (Y/N) ");
reply = myScanner.findInLine(".").charAt(0);
if (age >= 12 && age < 65) {
price = 9.25;
}
if (age < 12 || age >= 65) {
price = 5.25;
}
if ((reply == 'Y' || reply == 'y') &&
(age >= 12 && age < 65)) {
price -= 2.00;
}
System.out.print("Please pay $");
System.out.print(price);
System.out.print(". ");
System.out.println("Enjoy the show!");
}
}
Can anyone tell me the reason this error happen and a solution to this problem.
Thanks.
Re: Exception in thread "main" java.lang.NullPointerException Problem!!
Code:
reply = myScanner.findInLine(".").charAt(0);
This line calls the charAt() method on the value returned by the findInLine() method. So to split it into 2 lines of code make the call to the findInLine() method and assign the returned value to a local String variable and then make to call to the charAt() method on the String variable.
You will then need to insert a test for null in between these two lines.
Re: Exception in thread "main" java.lang.NullPointerException Problem!!
Hi everybody!
I am also new in Java programming. When I run my code, I get error code:
Exception in thread "main" java.lang.NullPointerException at mcds.Call.CheckSubscriberTokens(Call.java:118)
//Write the content of the request to the outputstream of the HTTP Connection.
out.write(b);
out.close();
//Ready with sending the request.
//Read the response.
InputStreamReader isr = new InputStreamReader(httpConn.getInputStream());
BufferedReader in = new BufferedReader(isr);
String Result = nodeLst.item(0).getTextContent(); --- IN THIS ROW I HAVE ERROR!!!
System.out.println("3");
// System.out.println("Odgovor_IN: " + Result);
//Write the SOAP message formatted to the console.
String formattedSOAPResponse = formatXML(outputString);
System.out.println(formattedSOAPResponse);
return Result;
}
//format the XML in your String
public String formatXML(String unformattedXml) {
try {
Document document = parseXmlFile(unformattedXml);
OutputFormat format = new OutputFormat(document);
format.setIndenting(true);
format.setIndent(3);
format.setOmitXMLDeclaration(true);
Writer out = new StringWriter();
XMLSerializer serializer = new XMLSerializer(out, format);
serializer.serialize(document);
return out.toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private Document parseXmlFile(String in) {
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(in));
return db.parse(is);
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
} catch (SAXException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
Bookmarks