I have a simple setter method for a Java property and null is not appropriate for this particular property. I have always been torn, in this situation: should I throw an IllegalArgumentException, or a NullPointerException? From the javadocs, both seem appropriate. Is there some kind of understood standard? Or is this just one of those things that you should do whatever you prefer and both i...
What are null pointer exceptions and what causes them in general?
When there is a post-condition, that return value of a method must not be null, what can be done ?
I could do
assert returnValue != null : "Not acceptable null value";
but assertions could be turned off !
So is it okay to do
if(returnValue==null)
{
throw new NullPointerException("return value is null at method AAA");
}
?
Or is it better to use a user-defined exc...
In a software company, does the team leader have to take responsibility for every error the subordinates do?
The client sent an angry email because some parts of the code were incomplete and not all logs were set into place. The error was done by one of my subordinates and he's the type of person that writes code in a hurry just to finish fast and "forgets" about writing good tested code.
Th...
When doing null checks in Java code, and you throw IllegalArgumentExceptions for null values, what kind of message template do you use?
We tend to use something like this
public User getUser(String username){
if (username == null){
throw new IllegalArgumentException("username is null");
}
// ...
}
What is better : "is null" or "was null", and why?
For me "is null" feels mo...
I've got plenty of these lying around, and I'm wondering if I'm going to face any trouble - or performance problems.
I have method A:
MyClass monkey;
...
if(monkey != null) {
...
}
Or method B:
boolean hasMonkey; //This is set to TRUE when monkey is not null
MyClass monkey;
...
if(hasMonkey) {
...
}
On a functional level, they both do the same thing. Right now, I'm using method A. Is t...
I want to ask why we don't have to add try-catch block to a RuntimeException while we should do that with other exceptions?
I mean like :
public class Main {
public static void main(String[] args) {
throw new RuntimeException();
}
}
Edit :
when I say : throw new RuntimeException(); it is so clear that there is an exception will happen ,so why the compiler doesn't forbid that ?
Take this method
/**
* @return List of group IDs the person belongs to
*
*/
public List<String> getGroups() {
if (this.getId().equals("")) return null;
}
I would like to throw exception instead returning null, what's the exception to throw when an important parameter/dependency has not been set?
Possible Duplicate:
Java: Why aren't NullPointerExceptions called NullReferenceExceptions ?
Was encouraged by response for my previous question, hence this question. Hope it's cool.
We know that we do not have pointer concept in Java except that reference is passed in some cases. In-spite we have 'pointer' in NullPointerException (which btw is my fovorite !!)
Yes this has more t...
I like to know why do we call null string. Where Strings are objects in java and objects can not be null only references can be null.
Can anybody elaborate on it?
I'm having some problems reading data from XML and load it into an array. This is the code I have:
private void LoadMap(String path) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance ( );
Document document = null;
try {
DocumentBuilderFactory builder = DocumentBuilderFactory.newInstance();
builder.setIgnoringElementContentWhitespace(true);
document = bu...
we have a android application that works fine up to android 2.1, but when we try to release it for android 2.2 we get a nullpointerexception. This happens when we're parsing a xml, on the following line of code:
((node) getText.item(0).getNodeValue();
help appreciated!
I have a class User that contains attributes: nickname, ipAddress, sharedFolder. The idea is to have a user with these attributes and a list of files from a shared folder.
This is my code:
import java.io.*;
import java.util.*;
public class User {
String nickname;
String ipAddress;
static ArrayList<String> listOfFiles;
File sharedFolder;
String fileLocation;
pu...
I'm trying to locate the source of the error in this trace:
org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver logException: Handler execution resulted in exception
java.lang.NullPointerException
at com.wikistart.service.WikiServiceImpl.getWikis(WikiServiceImpl.java:548)
at com.wikistart.controller.WikiController.wiki(WikiController.java:88)
at sun.reflect.Nat...
I think my rsort() method should work right I always get an exception:
In Thread "main": java.lang.NullPointerExeception
at IntQueue.get(IntQueue.java:47)
at V5.main(V5.java:88)
Why am I getting this exception and how can I handle it?
IntQueue.java:
class IntQueue
{
public int get()
{
int res = fyrsti.tala; // <---- this is line 47 -----
n--...
How can I cast an Object to an int in java?
I have written this function which will set
val=max or min (if val comes null)
or val=val (val comes as an Integer or "max" or "min")
while calling i am probably sending checkValue(val,"min") or checkValue(val,"max")
public String checkValue(String val,String valType)
{
System.out.println("outside if val="+val);
if(!val.equals("min") && !val.equals("max"))
{
Syste...
I am developing in Eclipse a new Java project which uses an existing application (I have added its jar in my project build path). When I create an object of a class (say Model) from this existing application and use any of its methods,
Model model = new Model();
model.start();
I get the following error:
Exception in thread "main" java.lang.NullPointerException
at main.gui.mainwindow.MainWi...
I am following this great discussion at SO, titled: The case against checked exceptions , but I am unable to follow where exactly RuntimeException should be used and how it is different from normal Exceptions and its subclasses. Googling gave me a complex answer, that is, it should be used to deal with programming logic errors and should be thrown when no Exception should normally occur, such a...
So I'm pretty new to Java. Been taking a class at my college, this first semester is focussing on getting the syntax down and some of the basic ideas right while using a java library called ObjectDraw. Next semester we're going to start getting away from ObjectDraw and into core Java some more. Anyways I have run into a few problems where I need to use something similar to php's isset function....
How can i create a dangling pointer using Java?