I keep getting NullPointerException in the following code. The exception appears at this line System.out.println(myHeros.toString());  public class test {  public static void main(String[] args) {      Hero A = new Hero("Archer");     Hero B = new Hero("Bacca");     Hero C = new Hero("Chester");     Hero D = new Hero("Teemo");     Hero E = new Hero("Garen");      MyStack myHeros = new MyStack(10);      System.out.println(!(A instanceof Hero)); // Output: false;      boolean debug = true;     try {          myHeros.push(A);         if(debug) {             System.out.println("lalala");         }     }     catch(notAHeroException e) {         //e.printStackTrace();     }      System.out.println(myHeros.toString());   //This is the line that throws nullPointerException;      System.out.println(myHeros.getTop()); //(Debug) Output: 1, the Hero was actually pushed into myHeroStack;            }   }  This is the class containing the toString method. I really cannot see where possibly can cause a NullPointerException.  public class MyStack implements Stack{  public Hero[] myHeroStack; //instance field; can be from this class to the whole world; int top;                   //The combination of these instance fields represent all you know about your object and all you can use about your object;  public MyStack() {     myHeroStack = new Hero[5];     top = 0;  }  public MyStack(int numOfHero) {     myHeroStack = new Hero[numOfHero];       top = 0; }  @Override public void push(Hero h) throws notAHeroException{     //check the input;     boolean checkType = !(h instanceof Hero);     if (checkType) {        //if (!(h instanceof Hero))         throw new notAHeroException("Check again what did you add?!");     }      else {         myHeroStack[top] = h;         top = top + 1;     }  }  @Override public void pull(Hero g) throws emptyStackException { //check whether the element you want to throw actually exist;     //check whether the element exist     if(myHeroStack[top - 1] == null) {         throw new emptyStackException("You do have this hero in you stack!");      }     else {         Hero heroToPull = myHeroStack[top - 1];         System.out.println(heroToPull.getName());         myHeroStack[top - 1] = null;         top = top -1;      }  }  @Override public void peek(Hero p) {     System.out.println(p.getName()); }  public int getTop() {     return top; }  public String toString() {     //check whether the stack is null     if(top == 0) {         return ("You hero stack is empty");     }     else {         String AllMyHero = "";         for(Hero hero: myHeroStack) {                        AllMyHero = AllMyHero + "All my hero is " + hero.getName()+ "%n"; //hero is null point         }         return AllMyHero;     }   }   }  This is the Hero class, which is quite simple.  public class Hero {      private String name;      public Hero(String name) {         this.name = name;     } }   Thank you for all who takes a look at this question!
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?
How do you photograph rain? What are recommended settings for my Nikon Coolpix P1 to best portray rain?  Here's what I got. No retouching was applied.   The picture I took when the rain was heavy with the automatic settings:  The picture I took with the "Sports" program; by then, however, the rain was not as hard. Ugh, ugly gray strip at the bottom, that was the flat surface I put my camera on to get a stable picture.   My main problem is that the actual rain and hailstorm is very hard to see in the picture. The overall effect is, instead, of fog.    Follow-up from the thread, courtesy of more rain, now with less hailstorm and more thunder.  The feedback amounted to flash and faster shutter. I took a few photos and I'm having some trouble to reconstruct what I did to get each result. Two representative results:   Fast shutter, with some guest starring:I'm talking about that horrible building obviously. Faster shutter and flash:Kinda difficult to say those pictures were taken sixty seconds of each other. :)   I'm not sure, however, that either of this is really an improvement of the original results. I was given a laptop with a totally dead display. I do not want to pay to replace the broken display, but I would like to use the laptop as a server and connect to it via SSH to control it.  I have pulled out the internal hard drive from the broken laptop, and I want to install Debian GNU/Linux and the SSH server onto this HDD using a working Debian laptop.  However, the hardware in the two laptops is very different, and I am wondering what kind of problems I would run into when I plugged the drive back in to the laptop with the broken display because of this?  How would I make this work? What is the proper way for me to install a bootable copy of Linux onto a HDD that will be used by a computer with different hardware? How can I assign the output of a command to a shell variable? I want to assign the result of an expression (i.e., the output from a command) to a variable and then manipulate it – for example, concatenate it with a string, then echo it.  Here's what I've got: #!/bin/bash cd ~/Desktop; thefile= ls -t -U | grep -m 1 &quot;Screen Shot&quot;; echo &quot;Most recent screenshot is: &quot;$thefile;  But that outputs: Screen Shot 2011-07-03 at 1.55.43 PM.png Most recent screenshot is:   So, it looks like that isn't getting assigned to $thefile, and is being printed as it's executed. What am I missing? Reference: Why are my "special" Unicode characters encoded weird using json_encode? Child who entered under ESTA/VWP accompanying US citizen mother Browsing through a copy of Counterexamples in Analysis (Gelbaum  &amp; Olmsted), I'm quite enjoying the typeface used for 'script' letters (see links to examples below); visually it's somewhere between what I'd now call calligraphic and fraktur, and semantically it's used where now we'd use mathcal, mathfrak or mathbb.  I was therefore procrastinatiously looking for a LaTeX-usable font to include these characters, but couldn't seem to find any. I'm certain they're not unique to this text or publisher, so figured my lack of success was probably more to do with searching with the wrong keywords. If anyone has any ideas I'd be grateful. How to keep a constant baselineskip when using minipages (or \parboxes)? When a minipage is used to box a text paragraph, the first line of the minipaged text and the last line of the previous text paragraph are closer than two consecutive lines in a standard text paragraph; analogously, the last line of text inside the minipage and the first line of text after the minipage are also closer.  The following MWE illustrates this:  \documentclass{article} \usepackage{lipsum}  \newcommand\Text{Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.\textbullet}  \begin{document}  \Text  \noindent\begin{minipage}{\linewidth} \Text \end{minipage}  \Text  \end{document}   How can I guarantee a constant baseline-skip even if minipages (or \parboxes) are used? How do I use regular expressions in bash scripts? I want to check if a variable has a valid year using a regular expression. Reading the  I understand I could use the operator =~  Looking at the example below, I would expect to see "not OK" but I see "OK". What am I doing wrong?  i="test" if [ $i=~"200[78]" ] then   echo "OK" else   echo "not OK" fi Present continuous and simple future Working with systems with Perfect Multicollinearity I am working with a time-series dataset that is based on demand-supply dynamics with several variables.  THe sample data for one time period is: &gt; data # A tibble: 19 x 6    Pred_error_A Pred_error_B  Pred_error_C  Pred_error_D  Pred_error_E  Pred_error_Target                  &lt;dbl&gt;         &lt;dbl&gt;          &lt;dbl&gt;      &lt;dbl&gt;      &lt;dbl&gt;           &lt;dbl&gt;  1            -0.45           1.72          -0.170      1.85       -0.01          -0.7    2            -0.45           1.72          -0.170      2.35       -0.01          -1.2    3            -0.45           1.92          -0.170      2.35       -0.01          -1      4            -0.0500         0.37          -0.12       1.65       -0.01          -1.5    5             0             -0.840         -0.12       0.950      -0.01          -1.7    6             0             -0.200         -0.12       1.35       -0.01          -1.5    7             0             -0.460         -0.12       1.35       -0.01          -1.8    8             0             -0.520         -0.12       1.05       -0.01          -1.5    9             0             -0.340         -0.12       1.05       -0.01          -1.4   10             0             -0.340         -0.12       1.35       -0.01          -1.7   11             0             -0.110         -0.12       1.05       -0.01          -1.2   12             0             -0.110         -0.12       0.850      -0.01          -1     13             0              0             -0.12       0.350      -0.01          -0.4   14             0              0             -0.12      -0.15       -0.01           0.100 15             0              0             -0.12      -0.350      -0.01           0.300 16             0              0             -0.02       0          -0.01          -0.1   17             0              0              0          0           0              0     18             0              0              0          0           0              0     19             0              0              0          0           0              0   I was working on a regression for each time period with data and wanted to find the relative importance of regressors in explaining the target variable ( Pred_error_A through E and dependent variable is Pred_error_Target ) using relaimpo package in R. However, I soon realized I can't use regression setting because of near to perfect collinearity and hence no relative importance values. Is there a way to go around this? Or, can I use any other method (instead of regression)? Any suggestions will be useful. The predictors are near to perfect and each one is important. I can't exclude any single one and I need the relative importance of all 5 regressors. I am also trying XGBoost as an alternative. However, I am not sure if it takes care of multicollinearity. Drift velocity of electron I realised, reading another Phys.SE question about  that I don't really understand how  works. Particularly concerning, for a SCUBA diver.  The top answers to that question seem to claim that balloons get their "sense of down" from a pressure differential. They continue: when a car accelerates, the air at the back of the car becomes more dense, and at the front less dense, changing the plane of the pressure differential and so also, the balloon's sense of up. I find that extremely hard to credit. However, I realised that I don't really know why less dense things float in more dense things.  I'm fairly sure it's something to do with displacement of heavier things by lighter things, and I think pressure acting on the lighter thing's surface has something to do with it, but that's about it. Israel visa stamp issue I am an Indian national living in London. I need to travel to Israel for work. I will need to apply for a visa but I have been told that it will be stamped in my passport. I have read that Israel knows of the problems travellers face in other Arab countries so they issue a paper visa but the embassy in London told me that it will be stamped in. I travel often in Middle East for work and I do not want any issues in the future. Please advise.  Thanks. How many generator has a cyclic group of order n? This question led to a new package:      I want to reference to citations from other file. Similar to what xr package does to labels, but I want to apply it for the bibliography.  If I'm able to do \cite{someone} in the main file, and it creates let say the number [4], I want to do the same \cite{someone} in a second file, and also create the same number [4]. Similar to what xr does with the labels.  By the way, I tried to add the main.bbl file from the main file. However, that adds all the references to the end of the second file. I want to avoid this.  Is there any package that accomplish this? I have a training set with about 3000 positive instances and 3000 negative instances. But my test data set is pretty much un-balanced. The positive set only has 50 instances and negative has 1500 instances.This causes the precision very low. Are there any approaches to solve this problem? I use SVM to build classifier. Yield return from a try/catch block Methane’s absorption bands are centered at 3.2 and 7.2 microns -- far off the peak of the Planck spectrum for a 290 K blackbody near 16 microns.  Moreover, its absorption bands overlap with the water absorption region.  So why is methane considered a stronger greenhouse gas than carbon dioxide, which has an absorption band at 15 microns? I want to install Windows XP on my desktop computer (only XP), but my CD drive is not working.  I've only found installation tutorials on how to install it on the pen drive rather than from the pen drive.  How can I install Windows XP from a USB flash drive? I want to know where the Metro applications (Mail, People, Messages, ...) executable file path is.  Also, how can I run Mail application when my PC starts?