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

Thread: Array

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Unhappy Array

    Hello!

    I am pretty new with java, I've been reading about it the last month and well I am practicing, so please be patient with me!

    So, I have this array matrizAsignaturas and I am trying to ask the user for a number and depending on the int received then print the column information. I am not sure what I am doing wrong here, but I get (I already looked for this error, but not sure if a quite understand what it means):

    Exception in thread "main" java.lang.NullPointerException
    at TrabajoIntegracionII.Asignaturas.verMatriz(Asignat uras.java:30)
    at TrabajoIntegracionII.Asignaturas.preguntaMatriz(As ignaturas.java:27)
    at TrabajoIntegracionII.Main.main(Main.java:13)
    Java Result: 1

    Anyways here is my code:

    ------------------------------------
    package TrabajoIntegracionII;
     
    import javax.swing.JOptionPane;
     
     
    public class Asignaturas {
     
        String matrizAsignaturas[][];
        int Programa;
     
        public void matrizAsignaturas(){
     
            matrizAsignaturas = new String [4][1];
     
            matrizAsignaturas[0][0] = "Programación I";
            matrizAsignaturas[1][0] = "Soporte I";
            matrizAsignaturas[2][0] = "Arquitectura I";
            matrizAsignaturas[3][0] = "Word I";
        }
     
        String a = "";
        int verMatriz;
        public void preguntaMatriz(){
            Programa = Integer.parseInt(JOptionPane.showInputDialog(null, "Ingrese un número de programa: \n 0-Técnico en Programación \n 1-Especialista en Soporte \n 2-Arquitecto en Sistemas \n 3-Especilista en ambientes office"));
            verMatriz(Programa);
        }
        public int verMatriz(int Programa){
            if(Programa == 0){
                a = matrizAsignaturas[Programa][0];
            }
            System.out.println(a);
               return verMatriz;  
            }
     
        }
    Last edited by helloworld922; August 14th, 2013 at 03:49 AM. Reason: please use [code] tags


  2. #2
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Array

    Hello.
    Where is the starting point of execution in your program?
    Probably you did not invoke the method that initializes the array.
    So before accessing array please initialize it first.

    Syed.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Array

    What's the difference between String[4][1] and String[4]?

    To Syed's point: Is the method matrizAsignaturas() ever called? That is how the matrix is initialized, replacing the null values with the specified strings.

  4. The Following User Says Thank You to GregBrannon For This Useful Post:

    valam (August 14th, 2013)

  5. #4
    Member
    Join Date
    Jul 2013
    Posts
    219
    Thanks
    0
    Thanked 18 Times in 17 Posts

    Default Re: Array

    Hello.
    Q). What's the difference between String[4][1] and String[4]?
    A). String[4] refers to 5th element in a one dimensional array.
    String[4][1] refers to 2nd element of 5th row in a two dimensional array.

    Syed.

  6. The Following User Says Thank You to syedbhai For This Useful Post:

    valam (August 14th, 2013)

  7. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Array

    @Syed,

    Excellent answer, but my question wasn't clear. In DECLARING an array, what's the difference between:

    String[][] colArray = new String[4][1];

    and

    String[] rowArray = new String[4];

    And I wasn't really expecting an expert to answer. I thought thinking about it might help the OP think about and visualize the problem (and maybe see the solution) differently.

  8. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Array

    Quote Originally Posted by valam View Post
    I am not sure what I am doing wrong here
    The method verMatriz returns a value, which is the int verMatriz, which was never initialized. As a side note naming a variable exactly the same as a method may lead to confusion

  9. The Following User Says Thank You to jps For This Useful Post:

    valam (August 14th, 2013)

  10. #7
    Junior Member
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Array

    Thank you all for your answers...

    I think I see where the problem is... I will be back in a few after I try

    @jps You are right, I shouldn't be doing that. What exactly is the return anyways, not sure exactly why it is needed. I kinda understand the concept but not exactly what it does.

  11. #8
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Array

    I like your answer too. One IS 1D and the other 2D, but it's also added complexity for the same storage capacity and no benefit that I can see. I suggest avoiding unnecessary complexity whenever possible.

  12. #9
    Junior Member
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Array

    Ok, so I think I am pretty confused here... At the begining I wanted to create a 2D array, now thanks to your comments I realized it was actually 1D, and I fixed that already...

    Now, what I trying to do is to request a number from the user and depending on their answer (0-3), then It will show the info that is contained there...

    So some questions (I'm sorry if there are dumb and noob, but I'll get there I promise! )

    1 - preguntaMatriz() all it does is to get a number ( 0-3 ), now since the array is declared as String then can I mix both?, I know that I am not suppose to have in array different types (char, float, int, etc)

    2- After requesting the number, do I really need a loop? I mean, at this point we already know the position in the array we need, so not sure if the loop is needed in this case. I think it does not do anything. So I removed it, but still get the same error, so again I am more lost!

    3- When I read the error, it point to line 42 and 34, so it does lead to my question # 1, one is String and the other one is int.. is this what is causing the problem?

    Here is the code again:

    --------------------------------

    package Trabajo;

    import javax.swing.JOptionPane;


    public class Asignaturas {

    String matrizAsignaturas[];

    public int Programa;
    public String a = "";
    public int verMatriz;
    public int i=0;


    public void matrizAsignaturas(){

    matrizAsignaturas = new String[4];

    matrizAsignaturas[0] = "Programación I";
    matrizAsignaturas[1] = "Soporte I";
    matrizAsignaturas[2] = "Arquitectura I";
    matrizAsignaturas[3] = "Word I";
    }


    public void preguntaMatriz(){

    i = Integer.parseInt(JOptionPane.showInputDialog(null, "Type a number to check the info (0-3) \n "
    + "\n 0-Técnico en Programación \n 1-Especialista en Soporte \n 2-Arquitecto en Sistemas \n 3-Especilista en ambientes office \n"
    + "\n"));
    verMatriz(i);
    }

    public int verMatriz(int i){
    for(i = 0; i < 4; i++){

    if(i == 0)

    a = matrizAsignaturas[i];

    }

    System.out.println(a);
    return i;

    }

    }

  13. #10
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Array

    Please post your code in code tags.

    We need to see the driving or testing program - the one with the main() method - to see and explain the errors you're seeing. An alternative would be for you to post the errors that you're seeing, copied and pasted just as they appear at your end.

  14. The Following User Says Thank You to GregBrannon For This Useful Post:

    valam (August 14th, 2013)

  15. #11
    Junior Member
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Array

    package Trabajo;
     
    import javax.swing.JOptionPane;
     
     
    public class Asignaturas {
     
    String matrizAsignaturas[];
     
    public int Programa;
    public String a = "";
    public int verMatriz;
    public int i=0;
     
     
    public void matrizAsignaturas(){
     
    matrizAsignaturas = new String[4];
     
    matrizAsignaturas[0] = "Programación I";
    matrizAsignaturas[1] = "Soporte I";
    matrizAsignaturas[2] = "Arquitectura I";
    matrizAsignaturas[3] = "Word I";
    }
     
     
    public void preguntaMatriz(){ 
     
    i = Integer.parseInt(JOptionPane.showInputDialog(null, "Type a number to check the info (0-3) \n "
    + "\n 0-Técnico en Programación \n 1-Especialista en Soporte \n 2-Arquitecto en Sistemas \n 3-Especilista en ambientes office \n"
    + "\n"));
    verMatriz(i);
    }
     
    public int verMatriz(int i){
    for(i = 0; i < 4; i++){
     
    if(i == 0)
     
    a = matrizAsignaturas[i]; 
     
    }
     
    System.out.println(a);
    return i; 
     
    }
     
    }

    --------------------------------

    package Trabajo;
     
    public class Main {
     
        public static void main (String args []){
     
            //Programas misProgramas = new Programas ();
            //misProgramas.Carreras(3);
     
            Asignaturas misAsignaturas = new Asignaturas();
            misAsignaturas.preguntaMatriz();
     
        }
     
    }


    Error message

    Exception in thread "main" java.lang.NullPointerException
    at Trabajo.Asignaturas.verMatriz(Asignaturas.java:42)
    at Trabajo.Asignaturas.preguntaMatriz(Asignaturas.jav a:34)
    at Trabajo.Main.main(Main.java:13)
    Java Result: 1

  16. #12
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Array

    Excellent!

    This is what we've been trying to explain to you: the method matrizAsignaturas() is never called, and it's responsible for populating the array matrizAsignaturas[] with values other than null. I suggest you call the method in a constructor:
        public Asignaturas()
        {
            matrizAsignaturas();
        }
    but there are other ways it could be done. For example, the main() method could call it:
        public static void main (String args [])
        {
     
            //Programas misProgramas = new Programas ();
            //misProgramas.Carreras(3);
     
            Asignaturas misAsignaturas = new Asignaturas();
            misAsignaturas.matrizAsignaturas()
            misAsignaturas.preguntaMatriz();
        }

  17. #13
    Junior Member
    Join Date
    Aug 2013
    Posts
    8
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Smile Re: Array

    yeiiii!!!, it was a simple mistake, but a HUGE mistake too... !! THANK YOU!!...

    I removed the loop I had, it was increasing the number i entered, so it was not helping at all....

    [code = java]

    public int verMatriz(int i){
    for(i = 0; i < 4; i++){

    if(i == 0)

    a = matrizAsignaturas[i];

    }

    System.out.println(a);
    return i;

    }
    [/code]


    and it is now,

    [code = java]

    public int verMatriz(int i){
    if(i == 0) {
    a = matrizAsignaturas[i];
    }
    else if (i == 1){
    a = matrizAsignaturas[i];
    }
    else if( i == 2){
    a = matrizAsignaturas[i];
    }
    else if(i == 3){
    a = matrizAsignaturas[i];
    }
    else {
    System.exit(0);
    }

    System.out.println(a);
    return i;

    }

    [/code]

    Yeiii! thank you!! I will continue with a 2D array now

    --- Update ---

    Thanks GregBrannon and everyone! I was able to create the 2D array and print it just as I wanted. . This thread is solved ! Thanks again!

Similar Threads

  1. [SOLVED] Return an array of Pairs that have the same length as the input array of Strings.
    By hemla in forum Object Oriented Programming
    Replies: 3
    Last Post: August 8th, 2013, 08:17 AM
  2. Java 1.4 Array issues - controlling array size for dynamic usage
    By doonan79 in forum Collections and Generics
    Replies: 5
    Last Post: June 18th, 2013, 11:53 AM
  3. compile errors when creating a 2nd array the same size as 1st array
    By javaiscool in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 26th, 2013, 09:35 PM
  4. Replies: 2
    Last Post: January 14th, 2013, 03:22 PM
  5. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM