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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 30

Thread: What's causing this Null Pointer Exception?

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default What's causing this Null Pointer Exception?

    It says that something is wrong when I call drawChars().

    I don't know what offset means. I'm assuming it means starting index.

       import java.util.*;
       import java.awt.*;
       import java.awt.event.*;
       import javax.swing.JOptionPane;
       import javax.swing.*;
       import java.io.*;
       import javax.swing.Timer;
     
       public class DadsBirthdayCard extends JFrame implements ActionListener
       { // beginning of program 
     
          private String birthdayString;
          private JPanel panel;
          private JScrollPane pane;
          private Timer timer;
          private int timesCalledCounter;
          private Random r1,r2,r3;
          private char[] chars;
          private char[] moreChars;
          private int red,green,blue;
     
          public void actionPerformed(ActionEvent e)
          { // beginning of method
     
             r1 = new Random();
             if (timesCalledCounter ==0)
             {
                birthdayString = "Happy Birthday Dad!";
                chars = birthdayString.toCharArray();
     
                red = r1.nextInt(256);
                green = r1.nextInt(256);
                blue = r1.nextInt(256);
                timesCalledCounter++;
             }
     
             else if (timesCalledCounter == 1)
             {
                birthdayString = "Happy birthday to you!";
                chars = birthdayString.toCharArray();
                red = r1.nextInt(256);
                green = r1.nextInt(256);
                blue = r1.nextInt(256);
                timesCalledCounter++;
     
             }
     
             else
             { // begin else
                birthdayString = "I love you, Paul.";
                chars = birthdayString.toCharArray();
                red = r1.nextInt(256);
                green = r1.nextInt(256);
                blue = r1.nextInt(256);
                timesCalledCounter = 0;
     
     
             } // end else
     
          } // end method
     
     
          public DadsBirthdayCard()
          {
             super("Happy Birthday Dad!");
             panel = new JPanel();
             pane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
             panel.setSize(5000,5000);
             pane.revalidate();
             timer = new Timer(0, this);
             timer.setDelay(5000);
     
             // r1 = new Random();
             r2 = new Random();
             r3 = new Random();
     
     
             setContentPane(pane);
          }
     
          public void paint(Graphics g)
          {
             super.paint(g);
     
             g.setColor(new Color(red,green,blue));
             g.drawChars(chars, 0, chars.length,300,300);
     
     
     
          }
     
          public static void main(String[] args)
          {
             DadsBirthdayCard dbc = new DadsBirthdayCard();
             dbc.setVisible(true);
          }
       }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What's causing this Null Pointer Exception?

    I'm sure you have heard this before. Seeing that you have 1219 posts.

    Please post it.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: What's causing this Null Pointer Exception?

    Well, I found what's causing it but not how to fix that.

    My char array is somehow being null.

    Also, will it keep changing with the String text or will it stay the same?

    I'm going to alter the code anyway so that it'll draw each individual character in a different color.

    Is the problem because I didn't initialize the char array BEFORE the actionListener in the code?

    If I put it after, then I'll run into more sets of problems maybe.

    I think I tried putting it after and it still gave me trouble.

  4. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: What's causing this Null Pointer Exception?

     

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at DadsBirthdayCard.paint(DadsBirthdayCard.java:87)
    at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:796)
    at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:713)
    at javax.swing.RepaintManager.seqPaintDirtyRegions(Re paintManager.java:693)
    at javax.swing.SystemEventQueueUtilities$ComponentWor kRequest.run(SystemEventQueueUtilities.java:125)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 597)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    null
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at DadsBirthdayCard.paint(DadsBirthdayCard.java:87)
    at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:796)
    at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:713)
    at javax.swing.RepaintManager.seqPaintDirtyRegions(Re paintManager.java:693)
    at javax.swing.SystemEventQueueUtilities$ComponentWor kRequest.run(SystemEventQueueUtilities.java:125)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 597)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)




    I know that my char array is null. Why, now that's the the question....

    Ok, so my String is null. That would be why the char array is null.

    But why is the String null?
    Last edited by javapenguin; July 2nd, 2011 at 04:55 PM.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What's causing this Null Pointer Exception?

    Is the problem because I didn't initialize the char array BEFORE
    Yes, that is exactly how you can get a NullPointerException.
    You must initialize a variable BEFORE you use it.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What's causing this Null Pointer Exception?

    Why, now that's the the question
    Its a question of where/when the code in your program is being executed. If the code that uses a variable before it is initialized is executed BEFORE the code that initializes the variable, then you must add code to test for the null value and react accordingly. IE do not try to use it.

  7. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: What's causing this Null Pointer Exception?

    Everything should be initialized BEFORE it's used.
    Here's the updated code
       import java.util.*;
       import java.awt.*;
       import java.awt.event.*;
       import javax.swing.JOptionPane;
       import javax.swing.*;
       import java.io.*;
       import javax.swing.Timer;
     
       public class DadsBirthdayCard extends JFrame implements ActionListener
       { // beginning of program 
     
          private String birthdayString;
          private JPanel panel;
          private JScrollPane pane;
          private Timer timer;
          private int timesCalledCounter;
          private Random r1,r2,r3;
          private char[] chars;
          private char[] moreChars;
          private int red,green,blue;
     
          public void actionPerformed(ActionEvent e)
          { // beginning of method
     
             birthdayString = "";
     
             r1 = new Random();
             if (timesCalledCounter ==0)
             {
                birthdayString = "Happy Birthday Dad!";
                chars = birthdayString.toCharArray();
     
                red = r1.nextInt(256);
                green = r1.nextInt(256);
                blue = r1.nextInt(256);
                timesCalledCounter++;
             }
     
             else if (timesCalledCounter == 1)
             {
                birthdayString = "Happy birthday to you!";
                chars = birthdayString.toCharArray();
                red = r1.nextInt(256);
                green = r1.nextInt(256);
                blue = r1.nextInt(256);
                timesCalledCounter++;
     
             }
     
             else
             { // begin else
                birthdayString = "I love you, Paul.";
                chars = birthdayString.toCharArray();
                red = r1.nextInt(256);
                green = r1.nextInt(256);
                blue = r1.nextInt(256);
                timesCalledCounter = 0;
     
     
             } // end else
     
          } // end method
     
     
          public DadsBirthdayCard()
          {
             super("Happy Birthday Dad!");
             panel = new JPanel();
             pane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
             panel.setSize(5000,5000);
             pane.revalidate();
             timer = new Timer(0, this);
             timer.setDelay(5000);
     
             // r1 = new Random();
             r2 = new Random();
             r3 = new Random();
     
     
             setContentPane(pane);
          }
     
          public void paint(Graphics g)
          {
             super.paint(g);
             System.out.println(Arrays.toString(chars));
             System.out.println(birthdayString);
             for (int i =0; i < chars.length -1; i++)
             {
                g.setColor(new Color(red,green,blue));
                g.drawChars(chars, i, i+1,300,300);
             }
     
     
     
          }
     
          public static void main(String[] args)
          {
             DadsBirthdayCard dbc = new DadsBirthdayCard();
             dbc.setVisible(true);
          }
       }

    The problem is still there.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What's causing this Null Pointer Exception?

    Then it is not being initialized.
    Add printlns after the statement that initializes it
    and before the statement that uses it.
    See what order they print out in.

  9. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: What's causing this Null Pointer Exception?

    Ok, Null Pointer Exception is gone but why is my array empty?

       import java.util.*;
       import java.awt.*;
       import java.awt.event.*;
       import javax.swing.JOptionPane;
       import javax.swing.*;
       import java.io.*;
       import javax.swing.Timer;
     
       public class DadsBirthdayCard extends JFrame implements ActionListener
       { // beginning of program 
     
          private String birthdayString;
          private JPanel panel;
          private JScrollPane pane;
          private Timer timer;
          private int timesCalledCounter;
          private Random r1,r2,r3;
          private char[] chars;
          private char[] moreChars;
          private int red,green,blue;
     
          public void actionPerformed(ActionEvent e)
          { // beginning of method
     
             birthdayString = "";
     
             r1 = new Random();
             if (timesCalledCounter ==0)
             {
                birthdayString = "Happy Birthday Dad!";
                chars = birthdayString.toCharArray();
     
                red = r1.nextInt(256);
                green = r1.nextInt(256);
                blue = r1.nextInt(256);
                timesCalledCounter++;
     
             }
     
             else if (timesCalledCounter == 1)
             {
                birthdayString = "Happy birthday to you!";
                chars = birthdayString.toCharArray();
                red = r1.nextInt(256);
                green = r1.nextInt(256);
                blue = r1.nextInt(256);
                timesCalledCounter++;
     
             }
     
             else
             { // begin else
                birthdayString = "I love you, Paul.";
                chars = birthdayString.toCharArray();
                red = r1.nextInt(256);
                green = r1.nextInt(256);
                blue = r1.nextInt(256);
                timesCalledCounter = 0;
     
     
             } // end else
             repaint();
          } // end method
     
     
          public DadsBirthdayCard()
          {
     
             super("Happy Birthday Dad!");
             birthdayString = "";
             panel = new JPanel();
             pane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
             panel.setSize(5000,5000);
             pane.revalidate();
             timer = new Timer(0, this);
             timer.setDelay(5000);
             chars = birthdayString.toCharArray();
             // r1 = new Random();
             r2 = new Random();
             r3 = new Random();
             System.out.println(birthdayString);
     
             setContentPane(pane);
          }
     
          public void paint(Graphics g)
          {
             super.paint(g);
             System.out.println(Arrays.toString(chars));
             System.out.println(birthdayString);
             for (int i =0; i < chars.length -1; i++)
             {
                g.setColor(new Color(red,green,blue));
                g.drawChars(chars, i, i+1,300,300);
             }
     
     
     
          }
     
          public static void main(String[] args)
          {
             DadsBirthdayCard dbc = new DadsBirthdayCard();
             dbc.setVisible(true);
          }
       }

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What's causing this Null Pointer Exception?

    why is my array empty?
    Can you show me the code that puts values in the array?

  11. #11
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: What's causing this Null Pointer Exception?

    birthdayString = "Happy Birthday Dad!";
    chars = birthdayString.toCharArray();

    Wait a minute....

    This line, when changed, added stuff to the array, though it's still not drawing it in any visible form.

    public DadsBirthdayCard()
    {
    super("Happy Birthday Dad!");
    birthdayString = "B";

    Also, I fixed an index error.

    Now it'll draw it, though it's not drawing the stuff I'm telling it to in the ActionListener.

    Also, my colors aren't changing, despite the ActionListener telling them to.
    Last edited by javapenguin; July 2nd, 2011 at 05:19 PM.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What's causing this Null Pointer Exception?

    Add a println after that statement to verify what is in the chars array:
    System.out.println("chars=" + java.util.Arrays.toString(chars));
    Then you'll know

  13. #13
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: What's causing this Null Pointer Exception?

    It's putting what I tell it in the constructor rather than what it was told in the ActionListener.

    How do I fix that?

    I already did the toString() thing. Now I think I know what's going on but I don't know how to change it.

    Also, still baffled about the color thing, though I feel it's likely the same thing, except, since the colors are all int values, (red,green,blue),

    it's just always making it new Color(0,0,0);

    Ok, problem identified.

    How do I fix it?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What's causing this Null Pointer Exception?

    it's just always making it new Color(0,0,0);
    Use variables in the new Color constructor that do not have 0 values.
    new Color(rd, grn, bl); // be sure rd, grn and bl are not all 0

    It's putting what I tell it in the constructor rather than what it was told in the ActionListener.
    No idea what you are talking about.
    Constructor?
    ActionListener?
    Did you tell it in Java or what language did you use?

  15. #15
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: What's causing this Null Pointer Exception?

    The Timer I'm using should be changing the String values, which should create a corresponding change in the char values.

    I have the class itself implementing ActionListener.

    However, it seems that my if and else if and else statements are not doing anything.

    Why is that?

    The problem appears to be scope related.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What's causing this Null Pointer Exception?

    Good idea to check the scope of your variables. Make sure you don't have definitions of variables with the same names at different levels of scope.

    it seems that my if and else if and else statements are not doing anything.
    Use println statement before the if statement to show the values of the variables being tested.
    Add printlns after the if and after the else to see which is being executed.
    The print out should help you see what the problem is.

  17. #17
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: What's causing this Null Pointer Exception?

    The problem is that it only will accept as valid what it is told in the constructor of the class for the value of birthdayString. It doesn't seem to care what it was told in the actionPerformed() method.

    Why is that?

    The printlns aren't pointing it out.

    I'm thinking it might be where my constructor and the actionPerformed() are located that's causing all this pain.

    Here's the code again:
       import java.util.*;
       import java.awt.*;
       import java.awt.event.*;
       import javax.swing.JOptionPane;
       import javax.swing.*;
       import java.io.*;
       import javax.swing.Timer;
     
       public class DadsBirthdayCard extends JFrame implements ActionListener
       { // beginning of program 
     
          private String birthdayString;
          private JPanel panel;
          private JScrollPane pane;
          private Timer timer;
          private int timesCalledCounter;
          private Random r1,r2,r3;
          private char[] chars;
          private char[] moreChars;
          private int red,green,blue;
     
          public void actionPerformed(ActionEvent e)
          { // beginning of method
     
            // birthdayString = "";
     
             r1 = new Random();
             if (timesCalledCounter ==0)
             {
                birthdayString = "Happy Birthday Dad!";
                System.out.println(birthdayString);
                chars = birthdayString.toCharArray();
     
                red = r1.nextInt(256);
                green = r1.nextInt(256);
                blue = r1.nextInt(256);
                timesCalledCounter++;
     
             }
     
             else if (timesCalledCounter == 1)
             {
                birthdayString = "Happy birthday to you!";
                System.out.println(birthdayString);
                chars = birthdayString.toCharArray();
                red = r1.nextInt(256);
                green = r1.nextInt(256);
                blue = r1.nextInt(256);
                timesCalledCounter++;
     
             }
     
             else
             { // begin else
                birthdayString = "I love you, Paul.";
                System.out.println(birthdayString);
                chars = birthdayString.toCharArray();
                red = r1.nextInt(256);
                green = r1.nextInt(256);
                blue = r1.nextInt(256);
                timesCalledCounter = 0;
     
     
             } // end else
             repaint();
          } // end method
     
     
          public DadsBirthdayCard()
          {
     
             super("Happy Birthday Dad!");
            // birthdayString = "Bodkd";
             panel = new JPanel();
             pane = new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
             panel.setSize(5000,5000);
             pane.revalidate();
             timer = new Timer(0, this);
             timer.setDelay(5000);
             chars = birthdayString.toCharArray();
             // r1 = new Random();
             r2 = new Random();
             r3 = new Random();
             System.out.println(birthdayString);
     
             setContentPane(pane);
          }
     
          public void paint(Graphics g)
          {
             super.paint(g);
             System.out.println(Arrays.toString(chars));
             System.out.println(birthdayString);
             for (int i =0; i < chars.length -2; i++)
             {
                g.setColor(new Color(red,green,blue));
                g.drawChars(chars, i, i+1,300,300);
             }
     
     
     
          }
     
          public static void main(String[] args)
          {
             DadsBirthdayCard dbc = new DadsBirthdayCard();
             dbc.setVisible(true);
          }
       }

    If I don't tell it what birthdayString is inside the constructor, it throws a NullPointerException.

    It shouldn't be doing that.
    Last edited by javapenguin; July 2nd, 2011 at 05:41 PM.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What's causing this Null Pointer Exception?

    As you suggested in your last post, check the scope of the variable. Define the variable at the class level and make sure there isn't another variable with the same name at an inner scope.

  19. #19
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: What's causing this Null Pointer Exception?

    Ok, my ActionListener is valid.

    Wait a minute.....


    Oops I did it again..... I forgot to start the Timer.


    Ok, that improved things, but now all my chars are running together, and they didn't before.

    Now it's giving me all these exceptions.
    Should I use a different char array each time?
     

    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: bad offset/length
    at sun.java2d.SunGraphics2D.drawChars(SunGraphics2D.j ava:2834)
    at DadsBirthdayCard.paint(DadsBirthdayCard.java:100)
    at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:796)
    at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:713)
    at javax.swing.RepaintManager.seqPaintDirtyRegions(Re paintManager.java:693)
    at javax.swing.SystemEventQueueUtilities$ComponentWor kRequest.run(SystemEventQueueUtilities.java:125)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 597)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    [H, a, p, p, y, , B, i, r, t, h, d, a, y, , D, a, d, !]
    Happy Birthday Dad!
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: bad offset/length
    at sun.java2d.SunGraphics2D.drawChars(SunGraphics2D.j ava:2834)
    at DadsBirthdayCard.paint(DadsBirthdayCard.java:100)
    at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:796)
    at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:713)
    at javax.swing.RepaintManager.seqPaintDirtyRegions(Re paintManager.java:693)
    at javax.swing.SystemEventQueueUtilities$ComponentWor kRequest.run(SystemEventQueueUtilities.java:125)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 597)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    [H, a, p, p, y, , B, i, r, t, h, d, a, y, , D, a, d, !]
    Happy Birthday Dad!
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: bad offset/length
    at sun.java2d.SunGraphics2D.drawChars(SunGraphics2D.j ava:2834)
    at DadsBirthdayCard.paint(DadsBirthdayCard.java:100)
    at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:796)
    at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:713)
    at javax.swing.RepaintManager.seqPaintDirtyRegions(Re paintManager.java:693)
    at javax.swing.SystemEventQueueUtilities$ComponentWor kRequest.run(SystemEventQueueUtilities.java:125)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 597)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    [H, a, p, p, y, , B, i, r, t, h, d, a, y, , D, a, d, !]
    Happy Birthday Dad!
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: bad offset/length
    at sun.java2d.SunGraphics2D.drawChars(SunGraphics2D.j ava:2834)
    at DadsBirthdayCard.paint(DadsBirthdayCard.java:100)
    at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:796)
    at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:713)
    at javax.swing.RepaintManager.seqPaintDirtyRegions(Re paintManager.java:693)
    at javax.swing.SystemEventQueueUtilities$ComponentWor kRequest.run(SystemEventQueueUtilities.java:125)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 597)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    Happy birthday to you!
    [H, a, p, p, y, , b, i, r, t, h, d, a, y, , t, o, , y, o, u, !]
    Happy birthday to you!
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: bad offset/length
    at sun.java2d.SunGraphics2D.drawChars(SunGraphics2D.j ava:2834)
    at DadsBirthdayCard.paint(DadsBirthdayCard.java:100)
    at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:796)
    at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:713)
    at javax.swing.RepaintManager.seqPaintDirtyRegions(Re paintManager.java:693)
    at javax.swing.SystemEventQueueUtilities$ComponentWor kRequest.run(SystemEventQueueUtilities.java:125)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 597)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    I love you, Paul.
    [I, , l, o, v, e, , y, o, u, ,, , P, a, u, l, .]
    I love you, Paul.
    Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: bad offset/length
    at sun.java2d.SunGraphics2D.drawChars(SunGraphics2D.j ava:2834)
    at DadsBirthdayCard.paint(DadsBirthdayCard.java:100)
    at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:796)
    at javax.swing.RepaintManager.paintDirtyRegions(Repai ntManager.java:713)
    at javax.swing.RepaintManager.seqPaintDirtyRegions(Re paintManager.java:693)
    at javax.swing.SystemEventQueueUtilities$ComponentWor kRequest.run(SystemEventQueueUtilities.java:125)
    at java.awt.event.InvocationEvent.dispatch(Invocation Event.java:209)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 597)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:269)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:184)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:174)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:169)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:161)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)


    Last edited by javapenguin; July 2nd, 2011 at 05:57 PM.

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What's causing this Null Pointer Exception?

    What statement is causing the error?

  21. #21
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: What's causing this Null Pointer Exception?

    g.drawChars(chars, i, i+1,300,300);

  22. #22
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: What's causing this Null Pointer Exception?

    So what's going on now?

    Likely I need more than one array, but how do I do that and keep the for loop structure?

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What's causing this Null Pointer Exception?

    g.drawChars(chars, i, i+1,300,300);
    Have you read the API doc for this method? What are the second and third args?

  24. The Following User Says Thank You to Norm For This Useful Post:

    javapenguin (July 2nd, 2011)

  25. #24
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: What's causing this Null Pointer Exception?

    Wait a minute, I'm telling it to draw too many characters perhaps.

    Let me try something.

    Ok, but the only way it looks clean is if it draws the whole word, which means the whole word gets the same color.

    I wanted each character to have a different color.

    How do I do that?

    If I try

    g.drawChars(chars, i, 1,300,300);

    it looks weird.

    As for what I mean by weird, try running it with that code.

    I can't really describe it, except perhaps as run together or something like that.


    ----Edit-----
    I believe the second argument is offset and the third one is number of characters drawn.
    Last edited by javapenguin; July 2nd, 2011 at 08:11 PM.

  26. #25
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: What's causing this Null Pointer Exception?

    Where are you drawing these letters? Look at arguments 4 and 5

Page 1 of 2 12 LastLast

Similar Threads

  1. Null Pointer exception
    By Demetrius82 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: June 2nd, 2011, 07:32 PM
  2. Null Pointer Exception Help !!
    By AlterEgo1234 in forum Member Introductions
    Replies: 1
    Last Post: March 27th, 2011, 10:07 AM
  3. [SOLVED] Null Pointer Exception
    By musasabi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 11th, 2010, 09:25 PM
  4. Null pointer exception
    By Wrathgarr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2010, 12:48 AM
  5. Null Pointer Exception
    By MysticDeath in forum Exceptions
    Replies: 2
    Last Post: October 24th, 2009, 01:49 PM