nullpointerexception

Best explanation for languages without null

有些话、适合烂在心里 提交于 2019-12-17 04:09:18
问题 Every so often when programmers are complaining about null errors/exceptions someone asks what we do without null. I have some basic idea of the coolness of option types, but I don't have the knowledge or languages skill to best express it. What is a great explanation of the following written in a way approachable to the average programmer that we could point that person towards? The undesirability of having references/pointers be nullable by default How option types work including strategies

findViewById() returns null for Views in a Dialog

廉价感情. 提交于 2019-12-17 04:08:27
问题 The problem is, no matter where or how I call for this layout's components, they always return null. setView(inflater.inflate(R.layout.search_layout, null)) This works fine. It displays the layout inside the Dialog , yet, the children are always returned as null by findViewById(R.id.some_search_layout_children) . I've tried cleaning my project multiple times, tried to implement another class for my Dialog , called findViewById() as a member of my main Activity , inside the initSearch() method

getActionBar() returns Null (AppCompat-v7 21)

寵の児 提交于 2019-12-17 04:06:08
问题 My app is crashing the minute I run it after I changed my AppCompat-v7 to 21.0.0 and Compiled with no problem. It gives me the error: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setIcon(int)' on a null object reference on this line: getActionBar().setIcon(R.drawable.ic_action_bar); It works with AppCompat-v7 20.0.0, but not with 21.0.0. 回答1: You need to call getSupportActionBar() on an ActionBarActivity. Do not call getActionBar() -- that is

Is null check needed before calling instanceof?

我只是一个虾纸丫 提交于 2019-12-17 03:45:20
问题 Will null instanceof SomeClass return false or throw a NullPointerException ? 回答1: No, a null check is not needed before using instanceof. The expression x instanceof SomeClass is false if x is null . From the Java Language Specification, section 15.20.2, "Type comparison operator instanceof": "At run time, the result of the instanceof operator is true if the value of the RelationalExpression is not null and the reference could be cast to the ReferenceType without raising a ClassCastException

NullPointerException through auto-boxing-behavior of Java ternary operator

半世苍凉 提交于 2019-12-17 03:42:26
问题 I tripped across a really strange NullPointerException the other day caused by an unexpected type-cast in the ternary operator. Given this (useless exemplary) function: Integer getNumber() { return null; } I was expecting the following two code segments to be exactly identical after compilation: Integer number; if (condition) { number = getNumber(); } else { number = 0; } vs. Integer number = (condition) ? getNumber() : 0; . Turns out, if condition is true , the if -statement works fine,

NullPointerException through auto-boxing-behavior of Java ternary operator

浪尽此生 提交于 2019-12-17 03:42:16
问题 I tripped across a really strange NullPointerException the other day caused by an unexpected type-cast in the ternary operator. Given this (useless exemplary) function: Integer getNumber() { return null; } I was expecting the following two code segments to be exactly identical after compilation: Integer number; if (condition) { number = getNumber(); } else { number = 0; } vs. Integer number = (condition) ? getNumber() : 0; . Turns out, if condition is true , the if -statement works fine,

Why static fields are not initialized in time?

故事扮演 提交于 2019-12-17 03:24:20
问题 The following code prints null once. class MyClass { private static MyClass myClass = new MyClass(); private static final Object obj = new Object(); public MyClass() { System.out.println(obj); } public static void main(String[] args) {} } Why are the static objects not initialized before the constructor runs? Update I'd just copied this example program without attention, I thought we were talking about 2 Object fields, now I saw that the first is a MyClass field.. :/ 回答1: Because statics are

Why does int num = Integer.getInteger(“123”) throw NullPointerException?

◇◆丶佛笑我妖孽 提交于 2019-12-17 02:35:22
问题 The following code throws NullPointerException : int num = Integer.getInteger("123"); Is my compiler invoking getInteger on null since it's static? That doesn't make any sense! What's happening? 回答1: The Big Picture There are two issues at play here: Integer getInteger(String) doesn't do what you think it does It returns null in this case the assignment from Integer to int causes auto-unboxing Since the Integer is null , NullPointerException is thrown To parse (String) "123" to (int) 123 ,

Android startCamera gives me null Intent and … does it destroy my global variable?

血红的双手。 提交于 2019-12-17 02:33:14
问题 I have the next problem: when I try to start my camera, I can take the picture, even save it on my sdcard, but when I'm going to get the path for showing it on my device I get errors. My global variables are 2 (I used 1 but 2 are better for making sure it's a strange error): private File photofile; private Uri mMakePhotoUri; and this is my start-camera function: @SuppressLint("SimpleDateFormat") public void farefoto(int num){ // For naming the picture SimpleDateFormat sdf = new

NPE while inflating layout (Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference)

浪尽此生 提交于 2019-12-16 22:50:12
问题 I keep getting a java.lang.NullPointerException when I try to use ScrollView in an activity. The weird thing is that I've used the exact same Scrollview setup in other activities. And all of a sudden I'm getting errors. I've tried cleaning the project and rebuilding. And I've tried the good old restart the program. I learned a lot about Null Pointer Exceptions today, but I haven't been able to figure out why I keep getting Rendering Problems . This is the java.lang.NullPointerException that