-4

We have a more than 3 Year old code which was working fine until now.

As per comments adding the simulated code , which is causing the null pointer exception.

public class TestMain {

    public static void main(String[] args) throws Exception{

        //List<MTMStagingRequest> request=init();
        List<String> names = new ArrayList<>();
        names = null;
        if(names != null){ // It throws NullPointerException
            System.out.println("He he");
        }else{
            System.out.println("No No");
        }
} 

Any Help appreciated.

jannis
  • 4,843
  • 1
  • 23
  • 53
Nsingh
  • 26
  • 3
  • What you've posted so far isn't actual java code. Please [edit] your question to include the relevant code (ideally a [mcve]). – azurefrog Feb 07 '20 at 20:44
  • I doubt that this code will throw anything since [it does not compile](https://ideone.com/7z3V8H). Also, [don't use raw types](https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it). Generics are around sind 2004, so unless the code is 10+ years old, there is no excuse for using raw types. – Turing85 Feb 07 '20 at 20:44
  • Umm why this inst java cde and why it does not compiles (i ommit lack of ;)? – Antoniossss Feb 07 '20 at 20:50
  • 2
    All in all this should not throw anyting. – Antoniossss Feb 07 '20 at 20:51
  • 1
    This prints No No 101% sure of that. – Antoniossss Feb 07 '20 at 20:52
  • As a java developer , I also understand it should not throw, The question is in which slightest scenario it can throw ... If it would have been that easy, believe me , I would have not posted it here ... It is happening but why .. I am having hard time to figure it out.. I cannot replicate it on my lower environments... – Nsingh Feb 07 '20 at 20:55
  • 1
    It can throw on custom JVM or manipulated bytecode or AOP - in general - in case of NOT THAT CODE beeing executed. Also it is non reproduceable as this runs just fine with NoNo on the output. – Antoniossss Feb 07 '20 at 20:56
  • 2
    Once again, [this code does not throw on any JCK compatible VM](https://ideone.com/VshViO). Please include the stack trace. I am sure the root cause is somewhere else. – Turing85 Feb 07 '20 at 20:59
  • Cannot paste actual code , due to company copyright policy. The code is not modified since last 3 years , It is working fine in lower and my local. But since yesterday it is failing in prod, out of nowhere ... I am thinking some thing changed at system level but no able to figure it out what that can be... – Nsingh Feb 07 '20 at 21:04
  • Your code won't be compiled. – Arvind Kumar Avinash Feb 07 '20 at 21:05

1 Answers1

2

This code will NOT throw NPE unless this is not run on some custom JVM. In your case, maybe you didn't rebuild your project and what you see is now what actually runs. Try clean and rebuilding your project first.

But overall, the code you have shown will not throw NPE.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
  • In Remtoe debug mode, it is coming to same place in code and goes to exception ... I have tried re build from Jenkins and still it fails... any thing I can check ... – Nsingh Feb 07 '20 at 20:58
  • 1
    Baaah its on remote debug - then you have no clue what actual code is executed. Even debugger evaluation can throw.... – Antoniossss Feb 07 '20 at 20:58
  • Maybe sources you use for debugging are different from the build you attach to. – jannis Feb 07 '20 at 20:59
  • nope, I am running in debug to know the cause and it is throwing null pointer exception at this place only.. – Nsingh Feb 07 '20 at 20:59
  • 1
    And again - this is not proof of NPE beeing thrown in the code. Debug can have different lines. – Antoniossss Feb 07 '20 at 21:00
  • 1
    Definitely the sources are out of sync. – jannis Feb 07 '20 at 21:02
  • Add some logging, deploy and see the output. I'm pretty sure there's something wrong with your deployment/build procedure – jannis Feb 07 '20 at 21:03
  • Although Java does not support pointers ```NullPointerException```, it uses pointers under the hood. In Java, all object variables point to a location in memory, where the data of the object are stored. ```null``` is a special value which means as much as "nothing". It does not point to any memory location. Whenever you attempt to read or write data or execute a method of the object while the variable pints to nothing, you get that ```NullPointerException```. However you can always assign null and compare the variable with null. – Stefan Feb 07 '20 at 21:08
  • I usually include the source code inside the deployed applications . For each *.class file, the programs have the related *.java file in the same folder (even when packed as *.jar, *.war or *.ear). Thus I can always take a look at the correct source code, regardless how old it is. – Stefan Feb 07 '20 at 21:12