I have two activities lets say Level Activity and Result Activity, so what happens is when I go from level -> level1 -> result activity I check if the score is > 4 if it is I unlock the next level in level activity by simply removing an imageview on a button in level activity by doing something like this in Result Activity  SharedPreferences pref = getSharedPreferences("MyPrefs", Context.MODE_PRIVATE);         final int score111 = pref.getInt("score111", 0);         v1.setOnClickListener(new View.OnClickListener(){             @Override             public void onClick(View v) {                 startActivity(new Intent(ResultActivity2.this,Lyricswho.class));                 if (score111 &gt;= 4) {                                            Intent act2= new Intent(ResultActivity2.this,Lyricswho.class);                     act2.putExtra("hideImageView", true);                     startActivity(act2);                 }             }         });     }   and then in Level activity   if(score111 &gt;=4 ){                 hideImageView = getIntent().getExtras().getBoolean("hideImageView");             if (hideImageView) {                 v.setVisibility(View.INVISIBLE);             }         }   It works fine for the first time but when I restart the app by closing it down and go to Level Activity it crashes and goes back to previous activity. The reason as I have figured out is the hideImageView gives null pointer error when I restart the app because it satisfies the condition that is score is greater then 4 (which previously it doesn't because the score is 0 so the code doesn't go in if condition) but there is nothing in hideImageView so it throws null exception. I tried using boolean variable so that if condition is ran only once but then my imageview appears again so I can't use that. How can I solve this problem? Been stuck there for days.
129
What are Null Pointer Exceptions (java.lang.NullPointerException) and what causes them?  What methods/tools can be used to determine the cause so that you stop the exception from causing the program to terminate prematurely?
63280
When I vote using the Android app, the button highlights briefly in orange as expected and then returns to the normal black (so both vote buttons are the same color). I expected the highlighting to remain in place to show my vote.  The post score reflects my vote, and if I tap the vote button again it cancels my vote (and updates the score again).  So my vote is there; it's just not shown.  If I vote, navigate back to the list of questions, and then return to the page where I voted, my vote shows in orange and is persistent. Votes I've previously cast show up and are persistent on first view; it's only new ones that behave like this.  By its nature, it's hard to take a meaningful screenshot.  I don't use the app a lot, so I don't know how new this is. My phone says I'm up to date (1.0.77).
41167
I am told that good developers can spot/utilize the difference between Null and False and 0 and all the other good "nothing" entities. What is the difference, specifically in PHP?  Does it have something to do with ===?
72615
I have to calculate the sum of related child object(contact) on field (Score1__c and Score2__c) to parent object Account object field(AccountScore1__c and AccountScore2__c)  my code is working on insert update undelete but not working with ****delete**** event  trigger ContactScoreRollup on Contact (after insert,after update,after undelete,Before Delete) {     Set&lt;Id&gt; accountIds = new Set&lt;Id&gt;();     List&lt;Account&gt; accountsToUpdate = new List&lt;Account&gt;();     List&lt;Account&gt; accountsToUpdate1 = new List&lt;Account&gt;();     if(trigger.isinsert || trigger.isundelete || trigger.isupdate){         for(Contact con :Trigger.new){             accountIds.add(con.AccountId);         }     }     if(Trigger.isdelete || Trigger.isupdate){         for(contact con : Trigger.old){             accountids.add(con.Accountid);         }         accountIds.remove(null);     }      for(AggregateResult ar :[SELECT AccountId,SUM(Score1__c) sumammount1 FROM Contact  where AccountId in: accountIds  GROUP BY AccountId]){          accountsToUpdate.add(new Account(Id = (Id)ar.get('AccountId'),AccountScore1__c= (Decimal)ar.get('sumammount1') ));     }      for(AggregateResult arr :[SELECT AccountId,SUM(Score2__c) sumammount2 FROM Contact  where AccountId in: accountIds  GROUP BY AccountId]){          accountsToUpdate1.add(new Account(Id = (Id)arr.get('AccountId'),Account_Score2__c= (Decimal)arr.get('sumammount2') ));     }     update accountsToUpdate;     update accountsToUpdate1; }
65925
Over the last day, every time I tried to check the Achievements the app crashes, even just after the app launch. Here's a snippet of the error report I sent:        I'm not sure if it's related, but my app also still displays the Embedded Engineering site which was closed as Unknown Site embedded which also crashes the app if selected.
41927
Consider the following one-person game:  A player starts with score $0$ and writes the number $20$ on an empty whiteboard. At each step, she may erase any one integer (call it $a$) and writes two positive integers (call them $b$ and $c$) such that $b + c = a$. The player then adds $b × c$ to her score. She repeats the step several times until she ends up with all $1$s on the whiteboard. Then the game is over, and the final score is calculated.  Example: At the first step, a player erases $20$ and writes $14$ and $6$, and gets a score of $14 × 6 = 84$. In the next step, she erases $14$, writes $9$ and $5$, and adds $9 × 5 = 45$ to her score. Her score is now $84 + 45 = 129$. In the next step, she may erase any of the remaining numbers on the whiteboard: $5$, $6$ or $9$. She continues until the game is over. Alya and Bob play the game separately. Alya manages to get the highest possible final score. Bob, however, manages to get the lowest possible final score. What is the difference between Alya’s and Bob’s final scores? I tried to &quot;decompose&quot; into a few numbers and I get the same scores. I am not sure how to prove the conjecture that any numbers will yield the same score no matter which path is taken.
73226
I'm having trouble restarting a 2D game when the user presses the "New Game" button.  Structure of my game:  The game has several 'screens': Opening screen, instructions screen, main game screen, and a game-over screen.  Each screen is an instance of a class: OpeningScreen class, GameScreen class, etc.  There is a class named Main. This class has three roles.   It's the JFrame (the window) of the program. It has the main method - first class to run. It has a method that change's screens.   When the program starts, this class runs. When it first runs:   Creates a new instance of itself (it's a JFrame-type class). Creates an instance of every 'screen-class'. The constructor of each 'screen-class' takes the instance of Main as an argument: (aka: GameOverScreen gameOverScreen = new GameOver(this)). Creates a new JPanel named mainPanel, and adds it to the JFrame. Any screen to be displayed will be contained in this JPanel (The screens are also JPanel-type classes).   How the changeScreen() method works:  The signature of changeScreen() is this: changeScreen(JPanel from, JPanel to).  Whenever a screen needs to be changed, the current screen that is displayed calls this method, like so: main.changeScreen(this, main.InstructionsScreen). It removes the previous screen from mainPanel and adds the new screen to mainPanel. As said, these screens are created when the program launches.  This is the method that changes screens:   public void changeScreen(JPanel from, JPanel to){      mainPanel.remove(from);     mainPanel.add(to);      mainPanel.revalidate();     mainPanel.repaint();  }   My problem:  After a game is finished, the Game class calls the changeScreen method: main.changeScreen(this, main.GameOverScreen) .  This replaces the game screen with a game over screen. Pressing the "New Game" button in the game-over screen, calls main.changeScreen(this, main.GameScreen) .  The problem: Pressing the "New Game" button in the game-over screen, indeed displays once again the instance of the Game class. But this is the old instance, so the game is not restarted. It's already finished.  I tried to do the following changes in changeScreen():  public void changeScreen(JPanel from, JPanel to){      mainPanel.remove(from);      if(to.equals(gameScreen)){         gameScreen = null;         gameScreen = new GameScreen(this);     }     mainPanel.add(gameScreen);      mainPanel.revalidate();     mainPanel.repaint();  }   This should reset the gameScreen object. But this creates a delay of about 10 seconds when pressing the "New Game" button.  What would be a good way of restarting the game?  Thank you
56415
I have four foreach loops that iterate through the collections and based on a condition do something.   Here is the code that I am writing now:  boolean breakFlag = false; String valueFromObj2 = null; String valueFromObj4 = null; for(Object1 object1: objects){   for(Object2 object2: object1){     // I get some value from object2     valueFromObj2 = object2.getSomeValue();     for(Object3 object3 : object2){       for(Object4 object4: object3){        // Finally I get some value from Object4.        valueFromObj4 = object4.getSomeValue();        // Compare with valueFromObj2 to decide either to break all the foreach loop        breakFlag = compareTwoVariable(valueFromObj2, valueFromObj4 );        if(breakFlag){break;}       } // fourth loop ends here       if(breakFlag){break;}     } // third loop ends here     if(breakFlag){break;}   } // second loop ends here   if(breakFlag){break;} } // first loop ends here   The main object (objects in the code) comes from a third-party provider SDK, so I cannot change anything on that portion. I want to ask the Stack&nbsp;Overflow community if there is a better approach to break all the four foreach loops. Or if there is any other way to refactor this code to make it more readable and maintainable.
16290
This is a canonical question and answer developed by the community to help address common questions. If you've been directed here, or your question has been closed as a duplicate, please look through the resources here and use them to shape more specific questions. To browse all canonical questions and answers, navigate to the  tag. This question and answer is owned by the community! If you can improve it and make it more useful, you're welcome to edit it. I am building a trigger, a unit test, or other Apex code. I'm working with sObject records, and trying to access related record information. For example, I might have a structure like this: Account a = new Account(Name = 'Test'); insert a;  Opportunity o = new Opportunity(Name = 'Donation', AccountId = a.Id); insert o;  System.assertEquals(o.Account.Name, 'Test', 'Correct Account associated'); // Assertion fails!  Or, in a trigger, I might be aiming for something like this: trigger ContactTrigger on Contact (after insert) {     for (Contact c : Trigger.new) {         if (c.Account.Name == 'Test') {             // Take some action here         }      } }  Why doesn't this work? Why am I getting null values, or NullPointerExceptions?
36803
Imagine that you want to do something, while the display in is action (not in sleep mode) and at the same time, you don't want to do anything else. This situation is like children lock, in which, all buttons fail to react, temporarily, to disable any kind of interaction by a child.  Is there a way to achieve this in Android? Do we have something like lock screen?
104197
When i create new User Account and login with that credentials, it gives me this problem if admin doesnt have set thier values yet !!!**  AllotmentList = [select id, Days_Remaining__c from PTO_Allotment__c where Year__c =:year and Employee__c=:EmployeeList[0].id LIMIT 100]; //access only Current user Allotment details                 //to handle Listoutof Bound Exception wrote if-else          if(AllotmentList.size()&gt;0)         {         requests.PTO_Allotment__c=AllotmentList[0].id;         requests.Days_Remaining__c=AllotmentList[0].Days_Remaining__c;         }         else         {             //write a code when AllotmentList is empty ....         }
56441
I am currently programming a trigger on Opportunities.   I created a test class for this and in my Sandbox I created an account, contact and opportunity. When I try to reference them   Opportunity Opp = [SELECT id from Opportunity where name = 'Test1'];    and then start my   Test.startTest();         Opp.StageName = 'PO in (100%)';          update Opp;         Test.stopTest();   It just errors and shows that Opp returns 0 rows. However when I paste my first bit of code in 'Execute Anonymous' it returns a row and the correct info.   Does this mean I have to create an account, contact, quote and all necessary fields in my test class?
61938
I was browsing  discussion () and noticed in the related posts lists the following difference in the score on a  question ().        I first noticed the related question be shown in linked nearly 3 minutes after  was left.    Hidden feature-request  Can we please stop displaying the same question from appearing in both lists? If a question is appearing under the linked posts list, it should not appear with the related questions too. Also, the difference in votes makes my eyes sore. It has been there for more than an hour now.
107240
Quite often, in programming we get situations where null checks show up in particularly large numbers. I'm talking about things like:  if (doc != null) {   if (doc.Element != null)   {     ... and so on   }   else     throw new Exception("Element cannot be null"); } else {   throw new Exception("document cannot be null"); }   Basically, the whole thing turns into an unreadable nightmare, so I'm wondering: is there an easier way to describe what I'm trying to do above? (In addition to null checks, I get things like string.IsNullOrEmpty from time to time.)  Accepted answer: I accepted the answer that has  because the approach described is innovative, and is precisely what I want. Thanks Shawn!
27078
I noticed for a while now the following syntax in some of our code:  if( NULL == var){    //... }   or  if( 0 == var){   //... }   and similar things.  Can someone please explain why did the person who wrote this choose this notation instead of the common var == 0 way)?  Is it a matter of style, or does it somehow affect performance?
46768
How often is my total score for a particular tag calculated. I've gotten several recent upvotes under android but my total score hasn't seem to have gone up. When I say total score, I mean like the total score displayed on this .
65166
It seems like my code works to check for null if I do  if ($tx)    or   if (isset($tx))   why would I do the second one when it's harder to write?