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 2 of 2 FirstFirst 12
Results 26 to 30 of 30

Thread: What's causing this Null Pointer Exception?

  1. #26
    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, so how far apart should I draw each one?

    Ok, the for loop is somehow drawing all of them together. I want it to draw each one separately but not just show one letter.

    I want

    TEXT LIKE THIS

    with each letter a different color and not the whole phrase in the same color.
    Nor do I want it to just show

    T

    E

    X

    T

    L
    I
    K
    E

    T

    H

    I

    S

    at different times either.
    Last edited by javapenguin; July 2nd, 2011 at 10:00 PM.

  2. #27
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,940
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

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

    There is/are method(s) that will give you the width of a character when it is drawn using a font.
    I don't remember what class it is in. You'll have to do some research.
    Get each character, get its width and advance x by that amount.

  3. #28
    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?

    That's not the problem now. I can space it out enough.

    But anyway, it's only applying it to the entire phrase, not the color change to each character.

    However, it's not a total loss using the drawChar() method as I can slant the words, something I couldn't do by merely using drawString().

    Still, having each character in a different color would be nice.

    I'm thinking the reason for that is how my for loop is coded

       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);
             System.out.println("Listeners:" + Arrays.toString(timer.getActionListeners()));
     
             timer.setDelay(5000);
             timer.start();
           //  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);
             g.setFont(new Font("Arial", Font.BOLD + Font.ITALIC, 30));
     
     
             for (int i =0; i < chars.length -1; i++)
             {
                g.setColor(new Color(red,green,blue));
                g.drawChars(chars, i, 1,300 + (6*i),300 + (6*i));
             }
     
     
     
     
     
          }
     
          public void drawCake(Graphics g)
          {
             g.setColor(new Color(184,79,223));
     
     
          }
     
          public static void main(String[] args)
          {
             DadsBirthdayCard dbc = new DadsBirthdayCard();
             dbc.setVisible(true);
          }
       }
    Last edited by javapenguin; July 2nd, 2011 at 10:14 PM.

  4. #29
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,940
    Thanks
    56
    Thanked 2,353 Times in 2,324 Posts

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

    the reason for that is how my for loop is coded
    yes that could be it

  5. #30
    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, though it'd be nice to know how to do that in the future, my Dad's birthday is tomorrow (I've kinda been busy lately and needed the internet to view all of those Graphics class methods) and I was often away from where I could get the internet.

    Still, I may resurrect this thread in the future to see how to draw each individual char in a different color.

    Anyone that knows how to fix my for loop to do this, please post the right code.

    For now, I'm marking this thread as "Solved".

Page 2 of 2 FirstFirst 12

Similar Threads

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