Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Exception in thread "main" java.lang.NullPointerException

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Exception in thread "main" java.lang.NullPointerException

    Hi Guys,
    I am new to Java and what I am trying to do is to access an object from one method to another this is my code bellow:
    public class ClassRc1 extends ConfigClass {
        public static DefaultSelenium browser;
    	public static void main(String[] args) throws FileNotFoundException, IOException, InterruptedException{
    		changephoto();
    	}
    		public static void login() throws FileNotFoundException, IOException, InterruptedException
    		   {
    		ConfigClass.connect();
    		String url=ConfigClass.config.getProperty("url");
    		String username=ConfigClass.config.getProperty("username");
    		String password=ConfigClass.config.getProperty("password");
     
    	    Selenium browser= new DefaultSelenium("localhost",4444 , "*firefox", "http://"+url);
    		browser.start();
    		browser.open("/");
    		browser.windowMaximize();
    		browser.type("//*[@id='email']", username);
    		browser.type("//*[@id='pass']", password);
     
    		}
    		public static void changephoto() throws FileNotFoundException, IOException, InterruptedException{
    			  login();
    			  browser.click("//*[@id='u_0_f']");	/* this line return null,ue of th how can i continue using the browser object from the previous method 	*/
    			}
     
    }
    in method changephot i want to use the browser object but it seems the object it returns null value, how can i keep the value of the browser object from the login method and continue using it. thank you guys in advance.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Edit: Thank you for taking the time and making the minimal effort required to acquaint yourself with the forum, learning how to post code properly, and then applying it to your very first thread requesting assistance.

    In the main() method, change this line:

    Selenium browser= new DefaultSelenium("localhost",4444 , "*firefox", "http://"+url);

    to:

    browser= new DefaultSelenium("localhost",4444 , "*firefox", "http://"+url);

    The first version creates a new, local (to main()) variable called browser. The second version initializes the variable browser that was declared outside the main() method. The browser variable created by the second version is now available for use by the changePhoto() method.

    Further, I'll advise you to get rid of everything 'static' other than the main() method. Running the functionality of an entire program from a static main() method is the technique used by very early beginners from which they should rapidly evolve. If you haven't yet made that necessary evolutionary step, I'll suggest you back up a few chapters in whatever book you're using as a study guide in order to write more code that gives you the understanding, experience, and confidence needed to make that leap.

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException

    Thank you very Much Brannon for your help dude you rock! and regarding the static type of functions , i did try to get rid of everything static but when i call the function from the main static function, i get an error and it asks me to change the changephot method to static, and i can't work around it! thank you very much sir for your help again and i wish you a merry Christmas!

Similar Threads

  1. I get the error Exception in thread "main" java.lang.NullPointerException?
    By jeremy28 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: October 3rd, 2013, 11:13 PM
  2. Replies: 1
    Last Post: April 7th, 2013, 03:40 PM
  3. Replies: 5
    Last Post: December 9th, 2012, 02:25 PM
  4. Exception in thread "main" java.lang.NullPointerException problem.......
    By Adam802 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 20th, 2012, 02:23 AM
  5. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM