Results 1 to 15 of 15
  1. #1
    robocop is offline Member
    Join Date
    Feb 2009
    Posts
    14
    Rep Power
    0

    Default [SOLVED] Trouble a method

    This is my first time for entering any information. Here goes...
    Here's my code:

    Here's what i have.

    public static void main (String [] args )
    {
    Pixel currPixel = null;
    Pixel oldPixel = null;
    Pixel newPixel = null;
    Picture p = null;
    Picture oldBackground = null;
    Picture newBackground = null;
    Picture newBg = null;
    Picture oldBg = null;
    String getMediaPath;
    Scanner keyboard = null;
    keyboard = new Scanner(System.in);
    String jpgFileName = null;
    double threshold = 0;//-1.0;
    String runAgain = "Y";
    double userPromt = 50.0;
    Picture lowerLimit = null;
    Picture upperLimit = null;

    This is part of my main method. My error occurs at line 210.
    code in main method

    208 newBg.hide();
    209 // now the swapBackground method will run
    210 threshold = validateDouble ("Enter a background and threshold double", 0.0, 200.0);
    211 p.swapBackground(oldBackground, newBackground, threshold);
    212 newBg.repaint();
    213 JOptionPane.showMessageDialog( null, "This is your updated picture");
    214 newBg.hide();

    error message:

    java.lang.NullPointerException
    at Picture.swapBackground(Picture.java:1653)
    at PA5BeckleyMain2ndTry.main(PA5BeckleyMain2ndTry.jav a:211)

    Can you help?

  2. #2
    pegitha is offline Member
    Join Date
    Apr 2007
    Location
    Indiana
    Posts
    83
    Rep Power
    0

    Default

    Can you post your validateDouble() method? Does it return the primitve double?

  3. #3
    robocop is offline Member
    Join Date
    Feb 2009
    Posts
    14
    Rep Power
    0

    Default

    pegitha,
    hope this helps
    //----------------------------------------------------------------------------------------------
    public static double validateDouble(String userPrompt, double lowerLimit, double upperLimit)
    //----------------------------------------------------------------------------------------------
    {
    Boolean validNumeric;
    double doubleValue = -1.0;
    String doubleString;
    String setOfValidDigits = "0123456789";

    do
    {
    validNumeric = true;
    doubleString = JOptionPane.showInputDialog(userPrompt);
    if (doubleString.length() == 0)
    validNumeric = false;
    if (doubleString.length() > 0 )
    for (int i = 0; i < doubleString.length(); i++)
    if (setOfValidDigits.indexOf(doubleString.charAt ( i ) ) < 0)
    validNumeric = false;
    if (validNumeric)
    doubleValue = Double.parseDouble(doubleString);
    }
    while (doubleValue < lowerLimit || doubleValue > upperLimit || !validNumeric);

    return doubleValue;

    }//End method double validateDouble()

    What am I supposed to enter in the input box
    threshold = validateDouble ("Enter a background and threshold double", 0.0, 200.0);
    when this pops up?
    Thanks,
    Robocop

  4. #4
    robocop is offline Member
    Join Date
    Feb 2009
    Posts
    14
    Rep Power
    0

    Default

    Still need help with the following code:


    JOptionPane.showMessageDialog( null, "You have chosen T");

    // the user will pick a picture
    fileName = FileChooser.getMediaPath("twoKidsWall.jpg");
    p = new Picture(fileName);
    p.show();
    JOptionPane.showMessageDialog( null, "This is the 1st picture that you picked");
    p.hide();
    // the user now picks the 2nd picture
    fileName = FileChooser.getMediaPath("wall2.jpg");
    oldBackground = new Picture(fileName);
    oldBackground.show();
    JOptionPane.showMessageDialog( null, "This is the 2nd picture that you picked");
    oldBackground.hide();
    // the user will pick a third picture
    fileName = FileChooser.getMediaPath("bridge.jpg");
    newBackground = new Picture(fileName);
    newBackground.show();
    JOptionPane.showMessageDialog( null, "This is the 3rd picture that you have chosen");
    newBackground.hide();

    // now the swapBackground method will run
    threshold = validateDouble ("Enter a background and threshold double", 0.0, 200.0);
    //p.swapBackground(oldBackground, newBackground, threshold);
    p.swapBackground(oldBg, newBg, 50.0);//threshold);

    Here is my error message:

    java.lang.NullPointerException
    at PA5BeckleyMain2ndTry.validateDouble(PA5BeckleyMain 2ndTry.java:289)
    at PA5BeckleyMain2ndTry.main(PA5BeckleyMain2ndTry.jav a:217)

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Java Code:
    if (doubleString.length() == 0)
        validNumeric = false;
    if (doubleString.length() > 0 )
        // etc
    showInputDialog can also return null, so you should perhaps test for that as well. Maybe that's the source of the NPE - it's hard to say as you didn't say which line 289 is.

    (This seems to be a double post - it might make things a little clearer if you direct people in the other thread to reply here where you have more code.)

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    What am I supposed to enter in the input box
    threshold = validateDouble ("Enter a background and threshold double", 0.0, 200.0);
    when this pops up?
    What do you mean? It's your code: surely you know what you intended to be entered into the dialog box...

  7. #7
    robocop is offline Member
    Join Date
    Feb 2009
    Posts
    14
    Rep Power
    0

    Default

    here is the rest of the data. I want to enter 50.0 in the dialog box. I'm new at this so please bear w/ me

    //----------------------------------------------------------------------------------------------
    public static double validateDouble(String userPrompt, double lowerLimit, double upperLimit)
    //----------------------------------------------------------------------------------------------
    {
    Boolean validNumeric;
    double doubleValue = -1.0;
    String doubleString;
    String setOfValidDigits = "0123456789";

    do
    {
    validNumeric = true;
    doubleString = JOptionPane.showInputDialog(userPrompt);
    289 if (doubleString.length() == 0)
    validNumeric = false;
    if (doubleString.length() > 0 )
    for (int i = 0; i < doubleString.length(); i++)
    if (setOfValidDigits.indexOf(doubleString.charAt ( i ) ) < 0)
    validNumeric = false;
    if (validNumeric)
    doubleValue = Double.parseDouble(doubleString);
    }
    while (doubleValue < lowerLimit || doubleValue > upperLimit || !validNumeric);

    return doubleValue;

    }//End method double validateDouble()

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    a double or cross post of the same question?

  9. #9
    robocop is offline Member
    Join Date
    Feb 2009
    Posts
    14
    Rep Power
    0

    Default

    I am trying to enter a double, 50.0, if this is what your asking

  10. #10
    robocop is offline Member
    Join Date
    Feb 2009
    Posts
    14
    Rep Power
    0

    Default

    its the same question

  11. #11
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    doubleString is not initialized. fix that.

  12. #12
    robocop is offline Member
    Join Date
    Feb 2009
    Posts
    14
    Rep Power
    0

    Default

    is this where I initialize it? and have i done it correctly?

    public static void main (String [] args )
    {
    Boolean validNumeric;
    double doubleValue = -1.0;
    String doubleString;
    String setOfValidDigits = "0123456789";
    Pixel colorDistance = null;
    Pixel currPixel = null;
    Pixel oldPixel = null;
    Pixel newPixel = null;
    Picture p = null;
    Picture oldBackground = null;
    Picture newBackground = null;
    Picture newBg = null;
    Picture oldBg = null;
    Picture mark = null;
    String getMediaPath;
    //Scanner keyboard = null;
    //keyboard = new Scanner(System.in);
    String fileName = null;
    double threshold = -1.0;
    String runAgain = "Y";
    double userPromt = 50.0;
    Picture lowerLimit = null;
    Picture upperLimit = null;
    String validateDouble = "threshold";

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I answered your question in the other thread. Please don't create two threads with the same question. This frustrates us to no end. Also, you've been here long enough to start using code tags. Please do so.

  14. #14
    robocop is offline Member
    Join Date
    Feb 2009
    Posts
    14
    Rep Power
    0

    Default

    I apologize for creating two threads. like i said i'm new @ this and don't understand all the terms. As far as being here long enough and using code tags, what is a code tag???

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    My standard post on code tags:

    When posting code here, please use code tags so that your code will retain its formatting and thus will be readable -- after all, your goal is to get as many people to read your post and understand your code as possible, right?

    To do this, highlight your pasted code (please be sure that it is already formatted when you paste it into the forum; the code tags don't magically format unformatted code) and then press the code button, and your code will have tags.

    Another way to do this is to manually place the tags into your code by placing the tag [cod&#101;] above your pasted code and the tag [/cod&#101;] below your pasted code like so:

    Java Code:
    [cod&#101;]
      // your code goes here
      // notice how the top and bottom tags are different
    [/cod&#101;]

Similar Threads

  1. [SOLVED] Method trouble &gt;:0!
    By PureAwesomeness in forum New To Java
    Replies: 10
    Last Post: 02-20-2009, 06:18 AM
  2. Trouble with method
    By BlueJ2008 in forum New To Java
    Replies: 2
    Last Post: 10-19-2008, 09:05 PM
  3. Trouble will calling a method
    By jonsamwell in forum New To Java
    Replies: 9
    Last Post: 08-22-2008, 10:16 PM
  4. having some trouble
    By Unknown1369 in forum New To Java
    Replies: 13
    Last Post: 07-21-2008, 11:52 PM
  5. Replies: 1
    Last Post: 03-08-2008, 06:41 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
  •