My floatpoint formulas fail to function if there is a null variable. IE if one of the input variables is null, then no result is displayed in the TextView that displays the result.
As far as I can tell there are 2 solutions to this.
One sets the null EditTexts to 0 if they are null:
if (charisma == null) {
charisma.setText(0);
} else if (charisma != null) {
charisma.setText(getcharisma);
}
The other ignores null variables in the equation:
try {
float FP = Float.parseFloat(pref.getString("fp", ""));
float CHR = Float.parseFloat(charismafpvalue.getText().toString());
float PER = Float.parseFloat(persuasionfpvalue.getText().toString());
float ELI = Float.parseFloat(elicitationfpvalue.getText().toString());
float AML = Float.parseFloat(animalhandlingfpvalue.getText().toString());
if (charismafpvalue == null) {
fpr.setText(Float.toString(FP - (PER + ELI + AML)));
} else if (charismafpvalue != null) {
fpr.setText(Float.toString(FP - (CHR + PER + ELI + AML)));
}
}
catch (NumberFormatException ignore) {}
The problem is that my code for both of these fails to function. The values aren't set to 0 if they are null and the formula failed to function despite the if() {} else if() {} code.
Any suggestions as to which is the better way to handle this? And how to implement it?
A clean way to solve this is to use Apache Commons NumberUtils#toFloat(String str, float defaultValue) method:
float chr = NumberUtils.toFloat(charismafpvalue.getText().toString(), 0.0f);
...
This sets chr to the default value if the conversion fails, so there is no need for try-catch either.
Improve you're String validation.
if (charismafpvalue != null && ! charismafpvalue.isEmpty()) {
// doSomething
}
Null cannot be casted to Float so you first need to check
if(charismafpvalue !=null && charismafpvalue.getText().toString()!=null && !charismafpvalue.getText().equals(""))
similarly to the other views
You could use
Float.valuOf(String s)
Which returns a float representation of "s" if available, or catch the NPE if "s" is null. Like so:
try
{
float someFloat = Float.valueOf(someString);
}
catch(NumberFormatException nfe)
{
}
catch(NullPointerException npe)
{
}
finally {}
More info here
Of course, this is here for completeness really. Null checking is always a winner! And Keppil's answer is very good also :)
Hope this helps
Related
I have x,y coordinates stored in a Point2D.Double type.
Code:
private Point2D[] block1 = new Point2D[99]
block1[0] = new Point2D.Double(12,14);
block1[1] = new Point2D.Double(15,16);
block1[2] = new Point2D.Double(20,20)
//etc all to 99.
//this can run about 10 times creating 10 different sets of x,y coordinates.
Want to iterate through all the arrays see if a specific coordinate is already there. If it is return true. Not sure on the best way to do it.
So I know that I will need a for/if loop.
Example: I want to check to see if (15,16) is there:
for(Point2D block[] : block1){
if(block.getX() == 15 && block.getY() == 16){
System.out.println("This is true");
}
}
So I want it to search through all the arrays to see if there is a (15,16). I can image this syntax is along the right lines but it is not right.
This approach will gets as close as possible to your desired syntax:
Point2D target = new Point2D.Double(15, 16);
for(Point2D block : block1){
if(target.equals(block)){
System.out.println("This is true");
}
}
By the way, you mentioned you want 10 times 10 different sets of coordinates, so you need to change the 99 to 100, otherwise you will crash the array:
Point2D[] block1 = new Point2D[100];
There's an error in this line:
for(Point2D block[] : block1){
if 'block1' is an array, while iterating 'block's type should be of the array's type. Namely, just Point2D - rather than Point2D[].
As a first step, try this snippet out:
for (Point2D block : block1) {
if (block.getX() == 15 && block.getY() == 16) {
System.out.println("Found it");
}
}
Almost there, now just extract that to a method:
public boolean containsPoint(Point2D[] points, int x, int y) {
for(Point2D block : points){
if(block.getX() == x && block.getY() == y){
return true;
}
}
return false;
}
...
/* calling the method */
if(containsPoint(points, 10, 10)) { // do stuff }
I am attempting to get the device's current battery level with the following:
Intent batteryIntent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int level = batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
int scale = batteryIntent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
Log.i(LOG_FILTER, "Battery level = " + (level*100)/scale);
// error check values
if (level == -1 || scale == -1) {
return -1;
} else {
return (level * 100) / scale;
}
The code seems to work and has never failed me, but I am getting a warning:
Method invocation 'batteryIntent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1)' may produce 'java.lang.NullPointerException'
This may never actually affect my application but as there is an end product depending on it, I am wondering, how might I accommodate this potential issue?
Is there a way to reorganize the above to achieve the same result (obtaining the battery's current state of charge)?
The Javadoc for registerReceiver states:
* #return The first sticky intent found that matches <var>filter</var>,
* or null if there are none.
So there is the potential that this will return you a null event. You already handle the case where the values are invalid ((level == -1 || scale == -1)), so I would recommend that you just check whether the intent is null, and return that value early:
if (batteryIntent == null) {
return -1;
}
I would throw a try/catch around it because you know it could throw the error, and then you can handle it appropriately if/when it ever does occur.
try {
//your code here
} catch (NullPointerException e) {
//handle the error here, maybe return -1?
}
I am working on an exercise, where I have to select a category(genre) of movie and based on my selection, the program will return a list of movies in that category from an ArrayList of objects.
My program works when typing out a category in string format. However I am trying to use a try catch block to also allow category selection by number.
My catch block is working, however my try block is not and returns nothing. Can someone help me determine what is wrong with my code? I am guessing there is something wrong with my parseInt assignment?
System.out.print("What category are you interested in?");
String catSel = sc.next();
try //Check category for Integer, otherwise catch
{
int numSel = Integer.parseInt(catSel);
if(numSel == 1)
{catSel = "animated" ;}
if(numSel == 2)
{catSel = "drama";}
if(numSel == 3)
{catSel = "horror";}
if(numSel == 4)
{catSel = "scifi";}
if(numSel == 5)
{catSel = "musical";}
if(numSel == 6)
{catSel = "comedy";}
else catSel = "";
//Check each movie for chosen category
for(int x = 0; x < list.size() - 1; x++)
{
if(catSel.equals(list.get(x).category))
System.out.println(list.get(x).movie);
}
}
catch (NumberFormatException e)
{
//Check each movie for chosen category
for(int x = 0; x < list.size() - 1; x++)
{
if(catSel.equals(list.get(x).category))
System.out.println(list.get(x).movie);
}
}
the way your if-clauses are structured, the else clause will be called whenever numSel is not 6, replacing catSel with the empty string.
You may want to add an else after each if block or replace all of them with a switch statement.
As #Dragondraikk suggested your if-else clauses are structured in a way which is not as per your expected result .
So either use in this way :
if(someCondition){
}
else if(someCondition){
}
...........................
do whatever you want to do
...........................
else{
}
Below is the way to use Switch Statement
switch(Integer.parseInt(catSel)){
case 1 :
do Something....
break;
case 2 :
do Something....
break;
case 3 :
do Something....
break;
case 4 :
do Something....
break;
case 5 :
do Something....
break;
case 6 :
do Something....
break;
default :
catSel="";
break;
}
Note : You can use try-catch block around this
Update
Advantage of Using Switch over If else
The problem with the if...else if... chain is readability , I have to look at every single if condition to understand what the program is doing. For example, you might have something like this:
if (a == 1) {
// stuff
} else if (a == 2) {
// stuff
} else if (a == 3) {
// stuff
} else if (b == 1) {
// stuff
} else if (b == 2) {
// stuff
}
(obviously, for a small number of statements like this, it's not so bad)
but I'd have no way of knowing that you changed condition variable half-way through without reading every single statement. However, because a switch limits you to a single condition variable only, I can see at a glance what's happening.
Another advantage is JumpTable
A switch is often compiled to a jump-table (one comparison to find out which code to run), or if that is not possible, the compiler may still reorder the comparisons, so as to perform a binary search among the values (log N comparisons). An if-else chain is a linear search .
Here is more about Switch Statement
I keep receiving a NullPointerException while trying to get a string from any array (that is encapsulated within a Vector). I cannot seem to stop the error from happening. It's got to be something simple, however I think that I have been looking at it for too long and I could sure use another set of eyes. Here is my code:
Vector<Event> details = vector.get(i).getEvent();
for (int x = 0; x < details.size(); x++) {
Event eDetails = details.get(x);
person = eDetails.getEventPerson();
place = eDetails.getEventPlace()[0];
time = eDetails.getEventTime()[0];
}
So when I try to get the item at position 0 in the array (when x is 0) that is returned from eDetails.getEventTime, a NullPointerException is thrown.
Now, when x is 0 I happen to know that the array element at position 0 of the getEventTime() array is an empty string, but it is NOT a null value. When x is 1 or 2, etc. I can retrieve the time just fine.
The problem is that I will still receive the NullPointerException when I try to do things like the following:
**System.out.println(eDetails.getEventTime.length);**
or
String result;
**if(eDetails.getEventTime[0] == null){**
result = "";
} else {
result = eDetails.getEventTime[0];
}
Any ideas?
Thanks!
Are you sure in your second example, it shouldn't be:
if(eDetails.getEventTime() == null)
Instead of:
if(eDetails.getEventTime[0] == null)
Are you making sure you leave the [0] off when you do the null check?
If the function eDetails.getEventTime() returns null, then you'll get a NullPointerException when you try to do eDetails.getEventTime()[0];
Seems that when you get details.get(0).getEventTime() the array returned is null.
The simplest way to figure this out is:
Vector<Event> details = vector.get(i).getEvent();
for (int x = 0; x < details.size(); x++) {
Event eDetails = details.get(x);
if (eDetails == null) {
throw new NullPointerException("eDetails on pos " + x + " is null");
}
person = eDetails.getEventPerson();
Something[] places = Details.getEventPlace();
if (places == null) {
throw ....
}
place = eDetails.getEventPlace()[0];
Something[] times = eDetails.getEventTime();
if (times == null) {
throw ....
}
time = eDetails.getEventTime()[0];
}
It may not look nice but at least it's informative.
How do I create a mutator method that adds two numbers but if the added number that is entered is negative, it will display an error message and not change the first number. Suggestions please.
public void restock(int newStockQuantity)
{
if(newStockQuantity < 0)
{
stockQ = stockQ;
}
{
system.out.println("Error not negative numbers");
}
else
{
stockQ = stockQ + newStockQuantity;
}
}
Well, for one thing you don't need the
stockQ = stockQ;
statement - that doesn't do anything.
Next is the problem of having multiple blocks in the "if" statement. If you did want to keep the no-op assignment, you could change your method to:
public void restock(int newStockQuantity)
{
if(newStockQuantity < 0)
{
stockQ = stockQ;
System.out.println("Error not negative numbers");
}
else
{
stockQ = stockQ + newStockQuantity;
}
}
With the no-op assignment removed, it's just:
public void restock(int newStockQuantity)
{
if(newStockQuantity < 0)
{
System.out.println("Error not negative numbers");
}
else
{
stockQ = stockQ + newStockQuantity;
}
}
Note the change from "system" to "System" as well - Java is case-sensitive.
That should compile and work.
Personally I would suggest throwing an exception if the method has an invalid argument instead of printing out a message to the console, but obviously it depends on the situation.
If you don't understand my first comment about having multiple blocks for the if statement, then I'd suggest going back to a good introductory Java book, and look at the syntax of if statements. It's slightly unclear which point you were having trouble with.
IMHO the best approach is the exception. Another point is to output the offending value, so a user of the method knows what the error is about. It also reveals errors in your check ;). Third point is to check for invariants first, bailing out with an exception or some appliable return-statement.
public void restock(int newStockQuantity) {
if(newStockQuantity < 0) {
throw new IllegalArgumentException("new stock " + newStockQuantity " must not be negative");
}
if (newStockQuantity == 0)
{
// nothing necessary, probably worth another exception?
return;
}
stockQ = stockQ + newStockQuantity;
}