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

Thread: Tutorials code not working with variables and arrays

  1. #1
    Junior Member
    Join Date
    Jan 2022
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Tutorials code not working with variables and arrays

    Hi everyone. I am new to Java but not new to programming, yet I can't get through any tutorials because the code keeps failing.

    The first thing I noticed is I need to write: // This line

    public static void main(String[] args) {
    Main here = new Main(); // This line
    System.out.println(here.testString);
    }

    In order to output variables because they are non-static? Tutorials don't get into that.

    Also, why can't I change the contents of an array with:

    testArray[1] = "black";

    Examples claim I can do this but Java 17.0.1 will not allow it.

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

    Default Re: Tutorials code not working with variables and arrays

    Please copy the full text of any error messages and paste them here.

    To access any non-static member of a class you need to use a reference to its instance.
    here is a reference to an instance of the Main class.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2022
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Tutorials code not working with variables and arrays

    package com.mycompany.helloworld;
     
    public class Main {
     
        String testString = "Welcome to the Matrix";
        int theLength = testString.length();
     
        String colors = "red, green, blue, grey, yellow, black";
        String[] splitColors = colors.split(", ");
     
        int[] testArray = {21, 32, 43, 54, 65, 76};
        String[] testArray2 = {"red", "blue", "yellow"};
        testArray2[1] = "black";
     
        public static void main(String[] args) {
            Main mod = new Main();
            System.out.println(mod.testString);
            System.out.println(mod.theLength);
            System.out.println(mod.splitColors[0]);
            System.out.println(mod.testArray[1]);
            System.out.println(mod.testArray.length);
            System.out.println(mod.testArray2[1]);
     
        }
     
    }


    --- Update ---

    cd C:\Users\pstad\Documents\NetBeansProjects\HelloWor ld; "JAVA_HOME=C:\\Program Files\\Microsoft\\jdk-17.0.1.12-hotspot" cmd /c "\"C:\\Program Files\\NetBeans-12.6\\netbeans\\java\\maven\\bin\\mvn.cmd\" -Dexec.vmArgs= \"-Dexec.args=${exec.vmArgs} -classpath %classpath ${exec.mainClass} ${exec.appArgs}\" -Dexec.appArgs= -Dexec.mainClass=com.mycompany.helloworld.Main \"-Dexec.executable=C:\\Program Files\\Microsoft\\jdk-17.0.1.12-hotspot\\bin\\java.exe\" \"-Dmaven.ext.class.path=C:\\Program Files\\NetBeans-12.6\\netbeans\\java\\maven-nblib\\netbeans-eventspy.jar\" -Dfile.encoding=UTF-8 org.codehaus.mojo:exec-maven-plugin:3.0.0:exec"
    Running NetBeans Compile On Save execution. Phase execution is skipped and output directories of dependency projects (with Compile on Save turned on) will be used instead of their jar artifacts.
    Scanning for projects...

    ----------------------< com.mycompany:HelloWorld >----------------------
    Building HelloWorld 1.0-SNAPSHOT
    --------------------------------[ jar ]---------------------------------

    --- exec-maven-plugin:3.0.0:exec (default-cli) @ HelloWorld ---
    Exception in thread "main" java.lang.ExceptionInInitializerError
    Caused by: java.lang.RuntimeException: Uncompilable code - cannot find symbol
    symbol: class testArray2
    location: class com.mycompany.helloworld.Main
    at com.mycompany.helloworld.Main.<clinit>(Main.java:1 )
    Command execution failed.
    org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
    at org.apache.commons.exec.DefaultExecutor.executeInt ernal (DefaultExecutor.java:404)
    at org.apache.commons.exec.DefaultExecutor.execute (DefaultExecutor.java:166)
    at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:982)
    at org.codehaus.mojo.exec.ExecMojo.executeCommandLine (ExecMojo.java:929)
    at org.codehaus.mojo.exec.ExecMojo.execute (ExecMojo.java:457)
    at org.apache.maven.plugin.DefaultBuildPluginManager. executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.e xecute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.e xecute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.e xecute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModul eBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModul eBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.single threaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStart er.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invo ke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invo ke (NativeMethodAccessorImpl.java:77)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl. invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:568)
    at org.codehaus.plexus.classworlds.launcher.Launcher. launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher. launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher. mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher. main (Launcher.java:347)
    ------------------------------------------------------------------------
    BUILD FAILURE
    ------------------------------------------------------------------------
    Total time: 1.533 s
    Finished at: 2022-01-09T22:34:43-05:00
    ------------------------------------------------------------------------
    Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.0.0:exec (default-cli) on project HelloWorld: Command execution failed.: Process exited with an error: 1 (Exit value: 1) -> [Help 1]

    To see the full stack trace of the errors, re-run Maven with the -e switch.
    Re-run Maven using the -X switch to enable full debug logging.

    For more information about the errors and possible solutions, please read the following articles:
    [Help 1] http://cwiki.apache.org/confluence/d...utionException

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Tutorials code not working with variables and arrays

    testArray2[1] = "black";
    That assignment statement needs to be inside of a method.
    Only initialization statements can be outside of a method.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2021
    Location
    Netherlands
    Posts
    13
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Tutorials code not working with variables and arrays

    Quote Originally Posted by Norm View Post
    That assignment statement needs to be inside of a method.
    Only initialization statements can be outside of a method.
    Makes sense. But I vaguely remember, from long ago, that in a class definition you could use

    static
    {
       // arbitrary code here
    }

    for code to be executed at class load time.

  6. #6
    Junior Member
    Join Date
    Jan 2022
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Tutorials code not working with variables and arrays

    Quote Originally Posted by Norm View Post
    That assignment statement needs to be inside of a method.
    Only initialization statements can be outside of a method.
    Thank you. That worked but I still don't know what I'm doing so I need to experiment. I still don't understand why no online courses or examples work out of the box. It's difficult to learn something when you can't even follow along with lessons because you are forced to go off on tangents to learn non documented material when you are doing simple beginner lessons.

Similar Threads

  1. Switch Statement with multpile char variables not working properly
    By ravinniaofcreed in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 22nd, 2017, 08:29 PM
  2. Replies: 1
    Last Post: August 5th, 2013, 02:49 PM
  3. [SOLVED] Working with Arrays
    By Vessel06 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 3rd, 2013, 06:45 AM
  4. using variables from other classes in arrays :/
    By mozyman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 30th, 2010, 11:26 PM

Tags for this Thread