Results 1 to 8 of 8
- 03-04-2013, 08:43 PM #1
Member
- Join Date
- Mar 2013
- Posts
- 4
- Rep Power
- 0
java.lang.NullPointerException in JUnit Parameterized.class
Hi guys, I hope somebody can guide me as to where I am going wrong with the code below.
I'm quite new to Java.
Basically I'm trying to run a JUnit parametrized class in Eclipse which reads in the parameters from a tab delimited file.
However I'm getting java.lang.NullPointerException error message.
This is occurring at the line "return calculate.readTabDelimFile(("C:\\JAVA LEARNING\\calculateNumbers.tab"))".
I have the method readTabDelimFile in the class CalculateForm.java
I've imported all the relevant classes and packages.
Below is the method readTabDelimFile which is in the class CalculateForm.javaJava Code:@RunWith(Parameterized.class) public class CalculateTwoNumbersTests { static SeleniumManager sm; static Selenium selenium; static CalculateForm calculate; private String number1; private String function; private String number2; private String answer; @BeforeClass static public void startServer() throws IOException{ sm = new SeleniumManager(); sm.start("http://www.compendiumdev.co.uk"); selenium = sm.getSelenium(); } @AfterClass static public void stopServer(){ sm.stop(); } public CalculateTwoNumbersTests(String num1, String function, String num2, String answer){ this.number1 = num1; this.function = function; this.number2 = num2; this.answer = answer; } @Parameters public static Collection data() throws NumberFormatException, IOException { [B] return calculate.readTabDelimFile(("C:\\JAVA LEARNING\\calculateNumbers.tab"));[/B] } @Test public void test_calculate_two_values() throws InterruptedException{ calculate = new CalculateForm(selenium); calculate.open(); calculate.setNumber1(this.number1); calculate.setFunction(this.function); calculate.setNumber2(this.number2); calculate = calculate.doCalculation(); assertEquals(this.answer,calculate.getAnswer()); } }
Any help in rectifying this will be greatly appreciated.Java Code:public Collection<String[]> readTabDelimFile(String filename) throws NumberFormatException, IOException { ArrayList<String[]> lines = new ArrayList<String[]>(); BufferedReader fh = new BufferedReader(new FileReader(filename)); String s; while ( (s=fh.readLine())!=null) { String f[] = s.split("\t"); lines.add(f); } fh.close(); return lines; }
Thanks
VastyLast edited by vastyjay; 03-05-2013 at 12:38 PM.
- 03-05-2013, 09:41 AM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: java.lang.NullPointerException in JUnit Parameterized.class
And after you've wrapped your code in [code] tags [/code], what is the full exception, including stack trace?
Please do not ask for code as refusal often offends.
- 03-05-2013, 12:45 PM #3
Member
- Join Date
- Mar 2013
- Posts
- 4
- Rep Power
- 0
Re: java.lang.NullPointerException in JUnit Parameterized.class
Thanks for your response, below is the full stack trace:
java.lang.NullPointerException
at com.automation.seleniumtutorials.chap31.CalculateT woNumbersTests.data(CalculateTwoNumbersTests.java: 74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Nativ e Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknow n Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Un known Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runRefle ctiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallabl e.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExpl osively(FrameworkMethod.java:44)
at org.junit.runners.Parameterized.allParameters(Para meterized.java:292)
at org.junit.runners.Parameterized.<init>(Parameteriz ed.java:282)
at sun.reflect.NativeConstructorAccessorImpl.newInsta nce0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInsta nce(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newI nstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.junit.internal.builders.AnnotatedBuilder.build Runner(AnnotatedBuilder.java:29)
at org.junit.internal.builders.AnnotatedBuilder.runne rForClass(AnnotatedBuilder.java:21)
at org.junit.runners.model.RunnerBuilder.safeRunnerFo rClass(RunnerBuilder.java:59)
at org.junit.internal.builders.AllDefaultPossibilitie sBuilder.runnerForClass(AllDefaultPossibilitiesBui lder.java:26)
at org.junit.runners.model.RunnerBuilder.safeRunnerFo rClass(RunnerBuilder.java:59)
at org.junit.internal.requests.ClassRequest.getRunner (ClassRequest.java:26)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestR eference.<init>(JUnit4TestReference.java:33)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestC lassReference.<init>(JUnit4TestClassReference.java :25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestL oader.createTest(JUnit4TestLoader.java:48)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestL oader.loadTests(JUnit4TestLoader.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRu nner.main(RemoteTestRunner.java:197)
- 03-05-2013, 01:53 PM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: java.lang.NullPointerException in JUnit Parameterized.class
'calculate' is null.
You only seem to initialise it in the test method, which is run after all the setup is complete, including @Parameter annotations.Please do not ask for code as refusal often offends.
- 03-05-2013, 02:17 PM #5
Member
- Join Date
- Mar 2013
- Posts
- 4
- Rep Power
- 0
Re: java.lang.NullPointerException in JUnit Parameterized.class
Can you give me an example of how you will fix this
Thanks
- 03-05-2013, 02:41 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: java.lang.NullPointerException in JUnit Parameterized.class
No idea.
I don't know what the test is, what the code is supposed to do that it is testing, what a reasonable pre-test set up would look like...anything that would allow me to give a reasonable answer.
I've pointed out where your error lies, so you now need to figure out how to fix it with respect to your test/code.Please do not ask for code as refusal often offends.
- 03-06-2013, 12:00 AM #7
Member
- Join Date
- Mar 2013
- Posts
- 4
- Rep Power
- 0
Re: java.lang.NullPointerException in JUnit Parameterized.class
Will appreciate sample solutions from forum members.
- 03-06-2013, 04:14 AM #8
Senior Member
- Join Date
- Jun 2007
- Location
- Bali, Indonesia
- Posts
- 696
- Rep Power
- 6
Similar Threads
-
java.lang.NullPointerException !! Pls help me with the calling other class.
By Decarle in forum New To JavaReplies: 6Last Post: 11-06-2012, 10:46 AM -
Java.lang.NullPointerException at
By bmbsage in forum Advanced JavaReplies: 2Last Post: 05-24-2012, 10:38 AM -
@RunWith(Parameterized.class) extended class error
By Richard3 in forum New To JavaReplies: 5Last Post: 01-31-2012, 02:47 PM -
Java.Lang.NullPointerException, but I am not sure why???
By buildakicker in forum New To JavaReplies: 22Last Post: 07-21-2011, 01:20 AM -
java.lang.nullPointerException
By KSUliz in forum New To JavaReplies: 10Last Post: 04-11-2010, 07:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks