cause of our problem: we have got a NullPointerException. You have probably seen these before. Our code is complaining because it was expecting to have some sort of object at some point and we didn't give it one. Next we have a series of lines of text that tell us where in the application the problem occurred.
We have quite a few lines of code that are referred to in this stack trace, most of them unfamiliar as we didn't write them. Let's start at the bottom and work our way up. We rst of all have the line of code that was running when our test failed; this is Thread. java line 745. This thread is using a run method (on ThreadPoolExecutor.java line 617) that is using a runWorker method (on ThreadPoolExecutor.java line 1142), and this carries on up the stack trace. What we are seeing is a hierarchy of code with all the various methods that are being used. We are also being told which line of code in that method caused a problem.
We are speci cally interested in the lines that relate to the code that we have written— in this case, the second and third lines of the stack trace. You can see that it is giving us two very useful bits of information; it's telling us where in our code the problem has occurred and what sort of problem it is. If we have a look at our code, we can see what it was trying to do when the failure occurred so that we can try and work out what the problem is. Let's start with the second line; rst of all it tells us which method is causing the problem. In this case it is com.masteringselenium.DriverFactory. clearCookies. It then tells us which line of this method is causing us a problem—in this case DriverFactory.java line 35. This is where our clearCookies() method tries to get a WebDriver instance from our WebDriverThread class, and then uses it to try and clear all the cookies.
Now, if you remember, we modi ed getDriver() to return a null instead of
a valid driver object. This matches up with the rst bit of information in our stack trace (the NullPointerException). Obviously we cannot call .manage(). deleteAllCookies() on a null, hence the null pointer error.
So why didn't it fail in WebDriverThread; after all, that's where the problem is? Passing a null around is quite valid. It's trying to do something with the null that causes the problem. This is why it also didn't fail on line 30 of DriverFactory. The getDriver() method just passes on what was returned from WebDriverThread, it doesn't actually try to do anything with it. The rst time that we tried to do anything with the null is when it failed, which was at line 35 of the DriverFactory class.
Comments
0 comments
Please sign in to leave a comment.