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: trouble with making a simple construct for an array of double type

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default trouble with making a simple construct for an array of double type

    Here is my partial code:

    public class Variable {


    private double[] array;

    public Variable(){
    array[0] = 0.0;
    }

    public String toString(){
    return array.toString();
    }

    public static void main(String[] args) {
    Variable Var1 = new Variable();
    System.out.println(Var1);
    }

    And I want to get the following output to simply be:
    [0.0]

    but for some reason there is still error. What am I doing wrong?


  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: trouble with making a simple construct for an array of double type

    for some reason there is still error
    Please copy and paste here the full text of the error messages.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: trouble with making a simple construct for an array of double type

    it just keeps prompting me to go to Debug mode, and does not provide a way to correct it, but i know that the error lies in
    "public Variable(){
    array[0] = 0.0;
    }"
    b/c that's where the debug first starts

  4. #4
    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: trouble with making a simple construct for an array of double type

    Try to compile the program to get a compiler error message. Copy and paste the full text of the compiler error message here.
    It's hard to suggest a fix without knowing what the problem is.

    Please Edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: trouble with making a simple construct for an array of double type

    well, i compiled the code many times, and there were no error messages while compiling; only while running.
    While running, however, it says that their is a NullPointerException on
    <array[0] = 0.0>

  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: trouble with making a simple construct for an array of double type

    NullPointerException
    Where do you assign a value to the array variable? It will have a null value if it is not assigned a value.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Nov 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: trouble with making a simple construct for an array of double type

    Here is the code again. Does this make it easier to figure out?
    public class Variable {
     
     
    private double[] array;
     
    public Variable(){
    array[0] = 0.0;
    }
     
    public String toString(){
    return array.toString();
    }
     
    public static void main(String[] args) {
    Variable Var1 = new Variable();
    System.out.println(Var1);
    }

  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: trouble with making a simple construct for an array of double type

    Where does the code assign a value to array? You need to add code that assigns a value to array.
    Take a look at the tutorial:
    http://docs.oracle.com/javase/tutori...ts/arrays.html
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Nov 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: trouble with making a simple construct for an array of double type

    ohhhhhhhhhhhhhhhhhhhhh I see what I did. i know it was a noob mistake, but thanks!!

    --- Update ---

    Now how would you be able to convert an array into a string?

  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: trouble with making a simple construct for an array of double type

    Please give an example of an array and the String you want to create from it.
    The String class has methods that can help you.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to convert from type double to type int?
    By rph in forum Object Oriented Programming
    Replies: 7
    Last Post: July 25th, 2011, 04:21 AM
  2. Array with Switch Construct
    By WhiteSoup12 in forum Loops & Control Statements
    Replies: 4
    Last Post: February 7th, 2011, 01:41 AM
  3. how to read an integer of DOUBLE datatype with type casting
    By amr in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 14th, 2010, 03:03 PM
  4. Typecasting of double variable to integer
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: December 5th, 2010, 03:41 AM
  5. Typecasting of double variable to integer
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 2
    Last Post: December 5th, 2010, 03:41 AM

Tags for this Thread