TesterHQ - The Evil Tester Blog Aggregator

Sep 1, 2017 - 3 minute read - Instagram

Why Java Null Pointer Promo - Instagram Post

Original Instagram Post

I’ve released a longer version of this video on Youtube, so if you need more details you can head off there via the link to the blog post at the end of this post (YouTube video is added at the bottom of that post). A common reason for Null Pointer Exceptions in Java is a variable redeclaration instead of instantiation. Learn what that means, how to avoid it, and how to spot it, in this presentation.

FAQ - why does my code throw a null pointer exception - common reason #1 Redeclaration - Using @BeforeClass or @Before can setup data for use in tests - Any ‘variables’ we instantiate need to be ‘fields’ rather than variables - We want to instantiate them in the setup method rather than redeclare them

Automatically Refactor prior to using @Before... - I might still want to use an @BeforeClass or @Before - To make the code readable - I could do that - but I don’t think I would have declared a new variable after the refactorings because I have a working code example that I’m moving.

In General - Try to write one test at a time so that if you have a problem it is easier to identify where the problem is - Try to write working isolated tests and then refactor to a more general solution when you need it - that way, you know it was working, so you just have to work backwards to find out what went wrong - Try to use automated IDE refactoring rather than move code around manually - Use the IDE syntax highlighting to help spot any issues

For more details see Blog Post: - http://blog.javafortesters.com/2017/08/faq-null-pointer-exception.html

I've released a longer version of this video on Youtube, so if you need more details you can head off there via the link to the blog post at the end of this post (YouTube video is added at the bottom of that post). A common reason for Null Pointer Exceptions in Java is a variable redeclaration instead of instantiation. Learn what that means, how to avoid it, and how to spot it, in this presentation. # FAQ - why does my code throw a null pointer exception - common reason #1 Redeclaration - Using @BeforeClass or @Before can setup data for use in tests - Any 'variables' we instantiate need to be 'fields' rather than variables - We want to instantiate them in the setup method rather than redeclare them # Automatically Refactor prior to using @Before... - I might still want to use an @BeforeClass or @Before - To make the code readable - I could do that - but I don't think I would have declared a new variable after the refactorings because I have a working code example that I'm moving. # In General - Try to write one test at a time so that if you have a problem it is easier to identify where the problem is - Try to write working isolated tests and then refactor to a more general solution when you need it - that way, you know it was working, so you just have to work backwards to find out what went wrong - Try to use automated IDE refactoring rather than move code around manually - Use the IDE syntax highlighting to help spot any issues For more details see Blog Post: - http://blog.javafortesters.com/2017/08/faq-null-pointer-exception.html

A post shared by Software Testing Tips & Videos (@eviltester) on