null « string « Java Data Type Q&A

1. Null \u0000 in Java String?

Are there any good reasons not to use \u0000 as a delimiter within a Java String? I would be encoding and decoding the string myself. This is for saving a list of ...

2. Why do we call null String?

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? ...

3. Why do I get a NullPointerException when comparing a String with null?

My code is breaking on the following line with a nullpointerexception:

 if (stringVariable.equals(null)){
Previous to this statement, I declare the stringVariable and set it to a database field. In this statement, I am ...

4. String[] initialized by null?

Hi i encountered this problem whereby when i initialized my String[], there seems to be a null in the String[] before i do anything. How do i initialized the String[] to ...

5. if (str1 == null) when throws a NullPointerException

In java, Does the following line have a possibility (even 0.01%) to throw a NullPointerException??

public static void handleRequest(String str1){
   if (str1 == null){  // this line throws NPE, ...

6. null = "" for a string

In Java are these 2 statements the same?

String a = null;
String b = "";
It feels a bit of a dumb question but have a complete mental block currently.

7. Why String.valueOf(null) is causing null pointer exception?

Why String.valueOf(null) is causing null pointer exception? Where the expected behaviour is to return "null" string.

String x = null;
This give a "null" string. but
will cause ...

8. string concatenation with null

I have the following code:

System.out.println("" + null);
And the output is null.
How does java do the trick in string concatenation?

9. Concatenating null strings in Java

Why does the following work? I would expect a NullPointerException to be thrown.

String s = null;
s = s + "hello";
System.out.println(s); // prints "nullhello"

10. Difference between null and "" Java String

What is the difference between null and the "" (empty string)? I have written some simple code like this:

String a = "";
String b = null;

System.out.println(a==b); // false
System.out.println(a.equals(b)); // false
And both statements return ...

11. Should Java String method (local) variables be initialized to null or ""?

What is the best practice for initializing a String method (local) variable to avoid the "Variable might not have been initialized" error in Java? String s=null; or String s=""; Does it make a difference? If so, ...

12. How to assign "Null" as a value to a string variable in Java

I am trying to import users from Active directory and in one of the cases, the user has a last name as Null. Can I have some pointers as to how ...

13. How to evaluate whether a string is null?

I have a string, which is read from a database. The string can be just "null". I need to decide whether it is null or not? Among the following ...

14. Getting value of one string variable correctly in Java if i do the debug but same values is null when i run the program

I am facing pretty weird problem here. I have one java project in eclipse. When I do the debug on program, It returns me value of one String variable properly, But ...

15. Value "null" by default in String

I have this class

public class Model {    
    private String a;
    private String b;

    public synchronized String getA() ...

16. getProperty method returns null for UTF-8 string

I have a problem getting localized value (a UTF-8 string) from the properties file.
Here is what i did ..

//properties file containing
public static void main(String[] args)
  File f = new ...

17. Java issue with String returning Null value, despite using Scanner class to input data

I am writing a program for a class that is very simple. All it requires of me is to print a grade sheet, and be able to input 3 pieces of ...

18. null String vs String "null"

Hi Ben Zhang , null is a key word used to initialize variables. In your case as you have declared the power variable and initialized it to null, mypower will have the same value. The difference between null and "null" is the later is a specific value similar to"xyz" and the earlier means it has no value, simply blank. If you ...

19. difference between null string and ""

It's ok but if run the below program it perfectly executes with out error saying that s is not initialized with string value. public class Test { public static void main(String[] args) throws Exception { step1 String s=""; step2 //String s2;//leads to compile time error because no value is initialized System.out.println(s.length()); } } I think step1 an step2 are same. :roll: ...

20. null or an empty String for "optional" data members

I have a Java class that models a business domain object. This business domain object has a number of fields that are represented as String-valued data members in the corresponding class. Some of those String-valued fields are not present in some object instances. Is it better to model such a non-existent (or non-available) field as having an empty String value ("") ...

21. How do I avoid null pitfalls when comparing Strings?

I'm having difficulty coming up with a good solution for comparing string values and avoiding the NullPointerException. Here's what I'm trying to do with half-way decent looking code I'll give the psuedocode: Compare two String values to determine if they are equal. String oldvalue; String newvalue; if oldvalue == newvalue then update = false What is a pretty way to write ...

22. string and object (null considered string?)

Hi Margaret, 'null' is really a 'null literal'. It's valid in Java and can be legally assigned to an Object; which is why new Sample(null) works. It gets treated as a String vs an Object because, when given a choice between two methods where the argument is valid for both parameters, the compiler will always choose the most specific parameter as ...

23. null string value

24. (String) null

25. null strings

27. String and null

28. string with null value

29. String is not null, then it is?

Hi, I'm using 2 classes, Child and Parent. In parent class, there is the abstract method setData() which is implemented in child class. My problem is... setData() instantiates the String s, but when button is pressed, it says s is null. However, if I invoke setData() in Child constructor whilst leaving Parent constructor empty, then a button press says s is ...

31. [Solved] String becomes null

I'm posting this here instead of the JDBC forum (sorry if that is the wrong guess!), because I don't think the JDBC portion of this is the cause of the problem. I have a String (tName) that is part of a data structure that is becoming null for some reason that I don't understand. Here is what happens: 1) I have ...

32. Difference between NULL and empty string

I think of an empty string ("") as a box that is empty. a null string means you don't even have a box!!! In all cases, you can refer to "the box that holds my comic books". The box may not exist, the box may exist but be empty, or the box may be full.

33. Absence of NULL in string ?

Because java.lang.String has other ways of specifying where it ends. The length() method is the main one. The reason C uses the NULL character* is because there is no other way to indicate how large an array is. Arrays in C have no hard bounds like arrays in Java have. You can easily try to read "elements" beyond its border. These ...

34. String add null to begining

to take it further (and pretend i have profound knowledge ) Always remember, classes are abstract entities(not to be confused with abstract classes). They are not supposed to execute any code or have sideeffects. I am not sayint they dont, but they are not supposed to. So this is one of the reason, most of the executable statements are tried to ...

35. Why does String null work this way?

Manish, Thanks for your reply... Still I am not able to get a clear understanding as why this is happening so. Aditya, When there is no string existing for the two given variables (a and b), how come null and null are getting concatenated??? ok... though I did not get the complete meaning of the replies... May I know whats the ...

36. How to parse "null" string to null.

Hi, Thanks, I was a little quick in posting the code, I have changed it so it will compile. Yes this is the code I currently use. I am convinced I have seen some method once, that does the same thing. Perhaps it was in the Apache commons library. We allow users of our custom logging application to provide a "null" ...

37. Cannot set a String to null?

I have the following codes and there are some error that I cannot resolve. (The following codes are in the main method) User u1 = new User("Alfred", 1); String userName = u1.getUserName(); int userId = u1.getUserId(); UniqueSecurityPin uspObj = new UniqueSecurityPin(userName, userId); do{ String pin = UniqueSecurityPin.generateSecPin(); //PIN GENERATION uspObj.setUsp(pin); //PIN IS SET TO USP object Calendar exe = Calendar.getInstance(); exe ...

38. String output showing "null"

Hey guys, beginner here....(Again) here is the latest code i have (2 .java files) and my output it showing "null" for the Part # and part Description when it runs, im new and i don't know what im not doing correctly. Java Code: // Ex3.13 (JHtP page104) //invoice a hardware store might use to represent an invoice //for an item ...

39. null v/s empty String(" ")

40. Trim for null string

41. String becomes null

I am having a problem where a string is being passed between several methods and then finally to a method in another class, I have tested the string at several points in my program and the string's value is becoming null at the point that it is passed into the method in the other class, here is a simplified example of ...

42. Null String

43. Difference between String=""; and String =null

There's no advantage to any of them that can be applied at all times. Which idiom you choose is dictated by how you need to scope the variable. There's no point assigning a reference to null, and then immediately assigning it to an object, but starting off with it assigned to null, before doing something else, is often useful

45. String is set but then instantly becomes null again, why?

You're resetting it to null every time the method is called. To be precise you're not even resetting it - it's discarded at the end of the method and reinitialized and reinstantiated. Laszlo's post was rather better put, I feel. Laszlo's post was too vague to understand. And I still don't get it because it can't be discarded at the end ...

46. Difference between String = "" and String = null

Because a null can't be dereferenced (you can't call methods on it). This is the case for all nulls, not just null as a value for String reference variables. The difference between "" and null is that "" is a zero-length string, and null is no string at all. For lack of a better analogy, consider this: take a book, and ...

48. How does Java Handle String Literals Setting to Null for Identical Values ?

Suppose, String s1="Java"; String s2="Java"; Now if i am not wrong only one value(bit pattern) "Java" exists in the heap and both s1 and s2 are pointing to it. If i set s1 to null, s1=null; Won't s2 be affected (it is not getting affected actually..) What could be the cause for this ? Thanks in Advance.

49. Why the string is null?

50. Difference in Null and Empty String

51. String str = null; have a value...

52. is null a String?

"null" is a String. null, the object reference, is not a string. However, without quotes it might still pick string unless you're explicitly casting null as an Object. Edit: Sorry, just noticed you tried the null reference test. You should get object if you called it with (Object)null. I think, according to the Java spec, it will automatically try to use ...

53. String reverting to 'null'

Your loop is explicitly designed to exit when that variable becomes null -- look at it carefully -- so it shouldn't be surprising that after the loop exits, that variable is null. Note that the variable is assigned to in the while-clause, so that's where it becomes null. You won't ever see it having the value null inside the body of ...

55. String is null - why?

56. empty string vs null string

When a String reference is set to null it does not point to any String at all. When a String reference point to an empty String it errr.... well it points to an empty String, that is to say a String containing no characters. So null is not a String but an empty String is.

57. Reader returns String "null"

58. Null + String obj