Results 1 to 8 of 8
  1. #1
    vastyjay is offline Member
    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0

    Exclamation 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.
    Java 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());
    		
    	}
    }
    Below is the method readTabDelimFile which is in the class CalculateForm.java

    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;
        		
            }
    Any help in rectifying this will be greatly appreciated.

    Thanks
    Vasty
    Last edited by vastyjay; 03-05-2013 at 12:38 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default 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.

    ** This space for rent **

  3. #3
    vastyjay is offline Member
    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0

    Default 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)

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default 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.

    ** This space for rent **

  5. #5
    vastyjay is offline Member
    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0

    Default Re: java.lang.NullPointerException in JUnit Parameterized.class

    Can you give me an example of how you will fix this

    Thanks

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default 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.

    ** This space for rent **

  7. #7
    vastyjay is offline Member
    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0

    Default Re: java.lang.NullPointerException in JUnit Parameterized.class

    Will appreciate sample solutions from forum members.

  8. #8
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: java.lang.NullPointerException in JUnit Parameterized.class

    Quote Originally Posted by vastyjay View Post
    Thanks for your response, below is the full stack trace:

    java.lang.NullPointerException
    at com.automation.seleniumtutorials.chap31.CalculateT woNumbersTests.data(CalculateTwoNumbersTests.java: 74)
    You can check what's producing the error in the CalculateTwoNumbersTests.java line number 74.

Similar Threads

  1. Replies: 6
    Last Post: 11-06-2012, 10:46 AM
  2. Java.lang.NullPointerException at
    By bmbsage in forum Advanced Java
    Replies: 2
    Last Post: 05-24-2012, 10:38 AM
  3. @RunWith(Parameterized.class) extended class error
    By Richard3 in forum New To Java
    Replies: 5
    Last Post: 01-31-2012, 02:47 PM
  4. Java.Lang.NullPointerException, but I am not sure why???
    By buildakicker in forum New To Java
    Replies: 22
    Last Post: 07-21-2011, 01:20 AM
  5. java.lang.nullPointerException
    By KSUliz in forum New To Java
    Replies: 10
    Last Post: 04-11-2010, 07:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •