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 10 of 10

Thread: NullPointerException in Java

  1. #1
    Member
    Join Date
    May 2008
    Posts
    35
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default NullPointerException in Java

    import java.util.Date;

    public class ex1{
    private static String[] bildate;
    int i;
    Date date1;

    public static void main(String args[])
    {

    String dat[] = null;
    String date1[]=null;
    for (int i = 0; i < dat.length; i++) {
    if((i%2)==0){
    date1[i]=dat[i];

    }else{
    bildate[i]=dat[i];
    }

    } String bildate[]=null;

    }
    }
    i am not able to understand why the NullPointer exception is coming??



  2. #2
    Senior Member
    Join Date
    May 2008
    Posts
    19
    Thanks
    0
    Thanked 9 Times in 4 Posts

    Default Re: NullPointerException

    There are all sorts of things wrong with this code!

    For starters, you are getting the first NullPointerException because you haven't specified dat's length.

    String dat[] = null

    For this to work It needs to be something like:

    String dat[] = new String[5];

    What exactly are you expecting the output to be?!

  3. #3
    Senior Member
    Join Date
    May 2008
    Posts
    19
    Thanks
    0
    Thanked 9 Times in 4 Posts

    Default Re: NullPointerException

    The only way your going to get this to compile is to do this:

    import java.util.Date;
     
    public class ex1 {
     
    	private static String[] bildate;
     
    	Date date1;
     
    	public static void main(String args[]) {
     
    		bildate = new String[5];
    		String dat[] = new String[5];
    		String date1[] = new String[5];
     
    		for (int i = 0; i < dat.length; i++) {
     
    			if ((i % 2) == 0) {
     
    				date1[i] = dat[i];
     
    			} else {
     
    				bildate[i] = dat[i];
    			}
     
    		}
     
    		String bildate[] = null;
     
    	}
    }

    But it doesn't actually do anything!

  4. #4
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: NullPointerException

    You're getting null pointer exceptions because everything is null. You created two arrays that are completely null and wondering where the null pointer is? Try giving some substance to your arrays.

    And FYI...you don't need to declare "i" before the for loop. The loop is doing it for you.

  5. #5
    Banned
    Join Date
    May 2008
    Posts
    25
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: NullPointerException

    When i code my software i always initialize a variable like this: String s = ""; etc saves a lot of trouble later on.

  6. #6
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: NullPointerException

    That may be good and all for small programs, but when you get into a large program or start using a lot of objects you don't want to initialize anything until you use it. Less memory allocated, less operations, and easier to debug in your IDE. Good for beginners who cant overcome these error, but it is bad practice and should be avoided.

  7. #7
    Banned
    Join Date
    May 2008
    Posts
    25
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: NullPointerException

    Quote Originally Posted by Chris.Brown.SPE View Post
    That may be good and all for small programs, but when you get into a large program or start using a lot of objects you don't want to initialize anything until you use it. Less memory allocated, less operations, and easier to debug in your IDE. Good for beginners who cant overcome these error, but it is bad practice and should be avoided.
    I find it easier to initialize objects but then again, there are people that don't like it. Nothing wrong with it though just my style of coding.

  8. #8
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: NullPointerException

    I agree that it is easier and it can help avoid unnecessary errors that noone wants to deal with, but saying there is nothing wrong with it is just naive. It uses memory when initialized and takes cpu cycles to initialize it. That is terrible practice when it is not needed. It is bloated code and is a common mistake to beginners. If you are writing small meaningless programs you wont notice, but you will if you ever write anything large.

  9. #9
    Banned
    Join Date
    May 2008
    Posts
    25
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: NullPointerException

    Quote Originally Posted by Chris.Brown.SPE View Post
    I agree that it is easier and it can help avoid unnecessary errors that noone wants to deal with, but saying there is nothing wrong with it is just naive. It uses memory when initialized and takes cpu cycles to initialize it. That is terrible practice when it is not needed. It is bloated code and is a common mistake to beginners. If you are writing small meaningless programs you wont notice, but you will if you ever write anything large.
    I've created quite some java applications (and non java software) but thats not the whole point. Your points are all valid but i'm assuming that when you create an object the object will used it later on so its better to initialize it beforehand instead of try catching the whole thing or wait for a compile error to show up the message. Also the memory usage of initializing an object is minimal. If you want microperformance java isnt the langauge to code in anyway.

  10. #10
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: NullPointerException

    Now i think that is something we can agree on. Java is way too bloated as it is. But then again i've seen other languages that claim to be better that have default values initialized into every variable. I'm just glad i dont have to worry about space and speed in my job (for the most part), but i do have to worry about goverment standards. I'm not sure what there opinion on variable inicialization is. Haven't gotten that far yet.

Similar Threads

  1. Java NullPointer Exception in Server chat program
    By Valtros in forum Exceptions
    Replies: 1
    Last Post: May 8th, 2009, 05:06 AM
  2. [SOLVED] NullPointerException in Poker's program
    By dean_martin in forum Exceptions
    Replies: 10
    Last Post: April 21st, 2009, 06:40 AM
  3. Replies: 3
    Last Post: February 26th, 2009, 02:04 AM