-1

error: java.lang.NullPointerException at StoryTests.testSaveToDB(StoryTests.java:57)

I am not sure what the error is but do need my test to pass, if anyone has any idea please let me know.

public class StoryTests {

    public StoryTests() {
    }

    ITmagazineSystem testStory;

    /**
     *
     */
    @BeforeClass
    public static void setUpClass() {
        ITmagazineSystem testStory = new ITmagazineSystem();
    }

    @AfterClass
    public static void tearDownClass() {
    }

    @Before
    public void setUp() {
    }

    @After
    public void tearDown() {
    }

    @Test
    public void testSaveToDB() {
        String title = "";
        String StoryID = "s200";
        String StoryTitle = "ASE";
        String StoryType = "Computing";
        String StoryContent = "aaaa";
        testStory.saveToDB(StoryID, StoryTitle, StoryType, StoryContent);

        try {
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/magazine", "root", ""); // database connection

            Statement stmt = (Statement) con.createStatement();

            //String searchTitle = "Select StoryTitle from stories where StoryTitle = '" + txtstoryTitle.getText();
            String searchTitle = "Select StoryTitle from stories where StoryTitle = '" + StoryTitle + "'";

            ResultSet rs = stmt.executeQuery(searchTitle);
            while (rs.next()) {

                title = rs.getString("StoryTitle");
            }
        }
        catch (Exception e) {
        }
        assertTrue(title.equals(StoryTitle));
    }
}
resueman
  • 10,572
  • 10
  • 31
  • 45
Ruf
  • 1
  • 3
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Erwin Bolwidt Mar 31 '15 at 15:01
  • still unable to figure it out :( – Ruf Mar 31 '15 at 15:09
  • @Ruf: in case you still not find out, we can't count line number in your code posted to find line 57. At least please give the exact line of problem – Hoàng Long Mar 31 '15 at 16:28
  • sorry new to this. the line 57 is testStory.saveToDB(StoryID, StoryTitle, StoryType, StoryContent); – Ruf Mar 31 '15 at 17:03
  • @HoàngLong i still have not figured it out – Ruf Mar 31 '15 at 17:40
  • @Ruf: testStory must be null then? You can do a simple check – Hoàng Long Mar 31 '15 at 17:41
  • so should it be: testStory.saveToDB(null);? still getting an error @HoàngLong – Ruf Mar 31 '15 at 17:49
  • I guess there is something wrong with your saveToDb method then, rather with your test – ascorbin Mar 31 '15 at 19:59
  • @Ruf: sorry that I don''t explain clearly, I mean testStory == null when the program runs to line 57. That creates the error you see. Could you please verify that? – Hoàng Long Apr 01 '15 at 00:55
  • Usually the NullPointerException points directly to the line which has the variable null. In this case testStory must be that varible. However, if that is not the case, could you please post the full stacktrace and we can check it easier – Hoàng Long Apr 01 '15 at 00:59
  • Thank you for your time. I tried testStory== null but still shows an error. The full stracktrace is java.lang.NullPointerException at StoryTests.testSaveToDB(StoryTests.java:52) @HoàngLong – Ruf Apr 01 '15 at 16:16
  • @Ruf: I doesn't mean that :( – Hoàng Long Apr 02 '15 at 14:52

2 Answers2

1

You are initializing a local variable in your setUpClass method. The global variable which has the same name will not be modified. This global variable is used in the test but will still be null and you get a NullPointerException. Do this instead (notice that the class type before testStory is gone):

@BeforeClass
public static void setUpClass() {
    testStory = new ITmagazineSystem();
}

In addition you have to make the global variable testStory static because the setUpClass method is static. You can't access non-static variables/methods in static methods.

Another option is to leave it non-static and move the initialization to the setUp method which is also non-static.

eee
  • 3,241
  • 1
  • 17
  • 34
  • excuse me, I don't understand very well. I didn't use BeforeClass before, but I read that the only difference between BeforeClass and Before is that BeforeClass will run only once before all the tests, while Before run again each time. I don't understand why we need to make testStory static? – Hoàng Long Apr 02 '15 at 15:30
  • 1
    You are right with your comment to BeforeClass and Before. I updated my answer to better clarify why to make `testStory` static. – eee Apr 02 '15 at 18:06
  • I can't believe that I miss the fact OP has declared a new local variable :| . You are right, that explains why testStory is null later – Hoàng Long Apr 03 '15 at 00:55
0

From the error you provide, 99% that the error lies here:

testStory.saveToDB(StoryID, StoryTitle, StoryType, StoryContent);

That means testStory is currently null, and causes NullPointerException because you try to use it.

Why it is null is another story. But I'm sure you know where to check.

Hint: very likely that your @BeforeClass function did not run.

Hoàng Long
  • 10,746
  • 20
  • 75
  • 124