Compile error java.lang.nullpointerexception [duplicate] - java

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
Sorry if I am asking this question that has been already asked, but I am kind of a noob at programming, I tried researching how to fix this bug but I am still having trouble trying to solve it. I am trying to compile this block of code here:
//Default constructor
public Game () {
potAmount = 100;
betAmount = 0;
}
public int getBetFromUser() {
//Introduction to the game
System.out.println("Welcome to Solitaire Dice Game..bet an amount\r\n" +
"\t-if you roll triples you win triple your bet,\r\n" +
"\t-if you roll doubles you win double your bet,\r\n" +
"\t-if you roll 10 or over, you keep your bet\r\n" +
"\t-otherwise you lose your bet\r\n" +
"A bet of 0 ends the game\r\n");
System.out.println("Your current pot is 100");
System.out.println("Enter your bet amount:");
betAmount = keyboard.nextInt();
return betAmount;
And I am trying to call it in my main class but I get this compile error:
Enter your bet amount:
Exception in thread "main" java.lang.NullPointerException
at Game.getBetFromUser(Game.java:26)
at Assign3.main(Assign3.java:9)

This is not a compile error, this is exception thrown in runtime.
You need to initialize keyboard variable before calling keybord.nextLine()

May be you know this thing. But for others I would re-iterate. There is difference between compile time error and run time error. When we compile it, what java compiler does is to check for syntax errors and if no syntax error is present then it would create a .class file out of it. If any syntactical error is there then the code will not compile. Whereas in runtime/execution errors, is an event or situation, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions.

Related

What is wrong here? I cannot run user input code [duplicate]

This question already has answers here:
Resource leak: 'in' is never closed
(14 answers)
Closed 8 months ago.
enter image description here
I just started learning java today. I am now stuck in this user input coding. I have tried many methods but I cannot run this user input code.
As the others already stated closing the scanner is best practice.
If you use Java 8 or higher you can do it the following way:
try(Scanner sc = new Scanner(System.in))
{
String name = sc.nextLine();
System.out.println(name);
}
Besides that your code already works! When you run the program it's waiting for input, so you need to write some text into the console-Window and press enter

string + operation error? [duplicate]

This question already has answers here:
After a string literal, all the + will be treated as string concatenation operator why? [duplicate]
(5 answers)
Closed 5 years ago.
So I made a simple program following the curriculum in Think Java. I was able to successfully make the code they asked for. But I encountered an error. When I add the operation at the end of the string, I get a different number than if I put the operation on its own line. Can anyone explain why? Where is that number in the last line of code coming from exactly?
Thank you, everyone!
public class Time{
public static void main(String args[]){
int hour=14;
int minute=22;
System.out.print("The number of seconds since midnight is: ");
System.out.println((hour*60*60)+(minute*60));
System.out.println("The number of seconds since midnight is: "+(hour*60*60)+(minute*60));
}}
**Thank you so much, Nongthonbam Tonthoi and everyone else (and those who directed me to the duplicate question). I probably wouldn't have found the answer because I didn't even know that was the same issue. Now I do though; I learned a lot from combining these responses. I really appreciate it.
Change this:
System.out.println("The number of seconds since midnight is: "+(hour*60*60)+(minute*60));
to:
System.out.println("The number of seconds since midnight is: "+ ((hour*60*60)+(minute*60)));
Add extra brackets.

Java ArrayIndexOutOfBoundsException [duplicate]

This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 5 years ago.
I wrote a Java program below:
int[] tar = {1,2,5};
for(int i=0 ; i<tar.length ; i++)
{
if(tar[i] - tar[i-1] > 2)
{
System.out.print("true");
}
}
why "tar[i] - tar[i-1]" doesn't mean any error? Isn't an error of ArrayIndexOutBoundsException?
It is an Exception and not error (if you mean it by compiler), cause Java knows about your array values only at run time.
Compiler cannot run your code and see that. What if at runtime you provided some good values at right indices ? That is the reason, compiler only checks the compiler level rules. It won't hint you about runtime behaviour.

Bukkit CommandException [duplicate]

This question already has answers here:
What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?
(26 answers)
Closed 6 years ago.
I just created a plugin that enables donors to vote on weather and time, there were no errors showing up in my IDE (Eclipse), but when I try to run it on my plugin test server, it gives CommandException, and I can't figure out what its problem is.
Here is my code:
http://pastebin.com/ksbfRDfT
Here is the Exception:
http://pastebin.com/fiKb4Vz3
I do need to hurry, because I am doing this for VoxelMC, and it needs to be done quick.
EDIT: The first command (startvote) is working now, however the second command is giving the same exception now. Am I not seeing something? I changed the old link to code to a new link to the code.
Bukkit commands work as follows:
/command args[0] args[1] args[2] args[3] args[4] args[5]
The exception is caused because you're using args[1], which corresponds to the second parameter in the command you typed in. You're most likely looking for args[0].
Another thing, you're using the == operator for String comparison, you should use String.equals(String);

Java - getting error on Hashmap.put [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
I wrote a simple program in JAVA having 4 classes:
Main - > CarFaxApp
GUI / Logic - > CarFaxFrame
Object -> Car
Validator -> CarFaxValidator
Once the program is running and I start to enter the necessary data in the textbox's for the car (String vin, String make, String model, int year). Everything works fine, but when I hit buttonADD(), I get a error.
private HashMap<String,Car>hmCar; //Hashmap to hold Cars, the key pair value consists of (Vin, Car Object)
The error happens on hmCar.put(car.getVin(), car); which I dont understand why, I tried debugging up to the point and car does hold the correct values.
Here a screen shot of the error:
the error is NullPointException, are you sure you do this
hmCar = new HashMap<String, Car>();
before you put the entry in.

Categories

Resources