I have below code which is giving null pointer exception in obtaining editText3 at line 4.
Intent intent = getIntent();
Double result = intent.getDoubleExtra(MainActivity.RESULT, 0);
setContentView(R.layout.activity_result);
EditText editText3 = (EditText)findViewById(R.id.editText3);
editText3.setText(Double.toString(result));
Can some please let me know what is the problem. I tried doing clean build but it didn't worked.
Thanks,
Rajan
You can double check if specify the wrong layout in setContentView() first.
Related
I am learning Kotlin from this [video][1] and at 35:45 he is running this code:
[enter image description here][2]
I ve tried to run exactly the same code:
fun main() {
val x = readLine()?:"1"
val y = readLine()?:"1"
val z = x.toInt() + y.toInt()
print(z)
}
But i get this error:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:662)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
at MainKt.main(Main.kt:4)
at MainKt.main(Main.kt)
Can somebody help me please? I am really a noob in kotlin (and sofware programming too) and i didn't found an answer on the web.
Thank you.
[1]: https://www.youtube.com/watch?v=5flXf8nuq60&t=302s
[2]: https://i.stack.imgur.com/nlHqi.jpg
[3]: https://i.stack.imgur.com/o7y2I.jpg
The Elvis operator evaluates to the right operand only when the left operand is null. The empty string "" is not the same as null.
readLine() returns null when it detects the "end of file". When reading from a file, this is obviously when reaching the end. When reading from stdin (the console's standard input), this is usually when you press Ctrl+D.
If you just press Enter, you are effectively inputting an empty line (""), not the "end of file".
If you want to get this kind of default value when you press Enter on the command line, then you should react to the empty string instead of null. As #lukas.j mentioned, one way to do this is to use ifEmpty to provide a default:
val x = readln().ifEmpty { "1" }
I'm new to Android Studio.
I'm getting
java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.String
com.alpha1.appname.rider.model.firebase.User.getName()' on a null
object reference.
Can anyone point me in the right direction. I can't seem to fix with the answers on the other similar questions.
\Here is the line that is producing the null pointer exception\
private void setDriverData() {
View navigationHeaderView = navigationView.getHeaderView(0);
TextView tvName = navigationHeaderView.findViewById(R.id.tvDriverName);
TextView tvStars = navigationHeaderView.findViewById(R.id.tvStars);
CircleImageView imageAvatar= navigationHeaderView.findViewById(R.id.imageAvatar);
tvName.setText(Common.currentUser.getName());
if(Common.currentUser.getRates() != null &&
!TextUtils.isEmpty(Common.currentUser.getRates()))
tvStars.setText(Common.currentUser.getRates());
if(Common.currentUser.getAvatarUrl()!=null &&
!TextUtils.isEmpty(Common.currentUser.getAvatarUrl()))
Picasso.get().load(Common.currentUser.getAvatarUrl()).into(imageAvatar);
This is the problematic line:
tvName.setText(Common.currentUser.getName());
The error message tells you that you cannot call getName of null. This means that Common.currentUser is null. Something being null means that it is not defined. Referring data members or calling methods of something that does not exist will yield this error. You can fix this issue by properly initializing Common.currentUser before that line executes, or defaulting it if it was not defined, like:
tvName.setText((Common.currentUser == null) ? "" : Common.currentUser.getName());
I'm trying to learn and create a basic sales tax app (inputs from item price and tax). In order to do this, I need to use a float or double for the decimals. I am very new to kotlin and don't know much, so I do not understand how I can get my num1 to parseDouble, I can clearly do it with the Integer data type with no errors. I've also tried toDouble which is giving me errors. Any help would be appreciated, thank you!
val calcButton = findViewById<Button>(R.id.calcButton)
calcButton.setOnClickListener {
var textPrice: EditText = findViewById(R.id.textPrice)
var textTax: EditText = findViewById(R.id.textTax)
var textTotal: EditText = findViewById(R.id.textTotal)
// error here: "Unresolved reference: parseDouble"
var num1 = Double.parseDouble(textTax.getText().toString());
// "parseInt" is working, however
var num2 = Integer.parseInt(textTax.getText().toString());
}
Try:
textTax.getText().toString().toDouble();
The reason you can't use Double.parseDouble is because Double in kotlin is not exactly the same as Double in Java, therefore, just calling Double will call kotlin's version, which doesn't have parseDouble static method.
If you want to use the Double from java.lang, you must specify the full package name:
java.lang.Double.parseDouble(textTax.getText().toString());
Or you can also do:
import java.lang.Double.*;
parseDouble(textTax.getText().toString());
I would suggest just sticking with Kotlin's versions, as they usually result in shorter code.
I'm trying to write a few methods for a class that I'm taking. When I run this method below, using my own personal testing scenarios, it's perfectly fine. But when I try to run the program with my professor's code via the driver class, I get AWT event queue-0. Please help! Thanks.
public Card getCardAt(int position) {
int valueCard=0, suitCard=0;
Card returnCard;
if (position>newNumberCards){
throw new RuntimeException("Invalid position number.");
}
valueCard = cards[position].getValue();
suitCard = cards[position].getSuit();
returnCard = new Card(valueCard, suitCard);
return returnCard;
}
The problem is with the line: valueCard = cards[position].getValue();
Thanks.
BTW cards is Card[].
So I'm trying to get a number input from a player in an RPG/survival type game, using showInputDialog to present the options, prompting the user to input a number. My problem is, I get a fatal error if they press cancel.
This is my current code:
String typeReader;
do{
typeReader = JOptionPane.showInputDialog(options);
}while(typeReader.isEmpty());
if (typeReader.isEmpty())
typeReader = "0";
charType = Integer.parseInt(typeReader);
and this is the error I get:
Exception in thread "main" java.lang.NullPointerException
at Game.main(Game.java:66)
Java Result: 1
BUILD SUCCESSFUL (total time: 14 seconds)
Ideally, if a user presses cancel the program would just read it as an empty String:
typeReader = "";
Can anyone help?
OK, you seem to be pretty new to this ;-)
First, you won't need the loop. Just write
String typeReader = JOptionPane.showInputDialog(options);
If the user clicks "Cancel", typeReader will be null afterwards. null is not an object, so you cannot call isEmpty() on it and you get the NullPointerException. Instead, you should check for null:
if (typeReader != null) {
...
}
You should read the Oracle tutorial on dialogs and maybe also the Javadoc.