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

Thread: bit array and register problem

  1. #26
    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: bit array and register problem

    When you create an array, it allocates storage for the number of elements that you specified for the array in the new statement. The values in the slots are null until you give them values.
    So Next you must assign values to each of the slots in the array.
    It's a two step process: create the array and fill the array with objects.

  2. #27
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: bit array and register problem

    I tried filling the array with this code:
    	public Register(int numberOfBits)
    	{
    		Bit[] registerBit = new Bit[numberOfBits];
    		for (i = 0; i < numberOfBits; i++)
    		{
    		registerBit[i].reset();
    		}
    	}
    When I run the debugger, I see that the program creates an array with numberOfBits of null values. That for loop should be giving a value of 0 for every i up to the last one but instead I get the error:

    ----jGRASP exec: java -Xnoagent -Djava.compiler=NONE -Xdebug -Xrunjdwp:transport=dt_socket,suspend=y,server=y RegisterTest

    ----jGRASP: connected to debugger.
    Exception in thread "main" java.lang.NullPointerException
    at Register.<init>(Register.java:119)
    at RegisterTest.main(RegisterTest.java:5)

    ----jGRASP wedge2: exit code for process is 1.

  3. #28
    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: bit array and register problem

    I tried filling the array
    The way to assign a value to an element in an array is this:
    String[] strArray = new String[21];   // define an array
    for(int i=0; i < strArray.length; i++){
        strArray[i] = new String();  // assign a  String object to the array element  
    }


    You have never put a Bit object into the array at index i so its value is null. The value of registerBit[i] is null.
    registerBit[i].reset();
    Last edited by Norm; August 31st, 2011 at 05:39 PM.

  4. #29
    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: bit array and register problem

    You need to read some more about arrays.
    Go to this site: Trail: Learning the Java Language: Table of Contents (The Java™ Tutorials)
    and Find Arrays

  5. #30
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: bit array and register problem

    for this Code

    	public Register(int numberOfBits, int valueStored)
    	{
    		Bit[] registerBit = new Bit[numberOfBits];
    		for (i = 0; i < numberOfBits; i++)
    			{
    			registerBit[i] = new Bit();
    			}
    		String valueBinary = Integer.toBinaryString(valueStored);
    		for (i = 0 ; i < registerBit.length - 1;i++)
    		{
    			int start = i;
    			int end = i+1;
    			int value;
    			String valueString = valueBinary.substring(start, end);
    			value = Integer.parseInt(valueString);
    			if (value == 1)
    				{
    					registerBit[i].reset();
    				}
    			else
    				{
    					registerBit[i].set();
    				}
    		}
    	}

    I want to create a toString() method that will return the string valueBinary. I tried doing this code:
    	public String showRegister()
    	{
    		return valueBinary;
    	}

    but the code doesn't recognize the valueBinary in the showRegister() method. Is there a way where I can pull something from another method into this one?

  6. #31
    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: bit array and register problem

    Is there a way where I can pull something from another method into this one?
    The valueBinary variable is defined local to the constructor. If you want to be able to use it in other methods, you need to move its definition to the class level.

    You can define it in the class and give it a value in a method.

  7. #32
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: bit array and register problem

    Is there a way to form a string from an array? I want to concatenate all the values in order from Bit[i] to Bit[bit.length]

  8. #33
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: bit array and register problem

    There are methods in the Java API that can convert an array into a String. However the format is probably not what you what. So you need to roll your own. Use a StringBuilder, iterate over the array, append each element, call toString when done.
    Improving the world one idiot at a time!

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

    ryu2le (August 31st, 2011)

  10. #34
    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: bit array and register problem

    a way to form a string from an array
    What do you want to see in the String?

  11. #35
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: bit array and register problem

    I got the string to work. I'm having an issue with the register now. Every time I create a new register, it saves over all the variables for the previous register. Is that a problem with the private variables?

  12. #36
    Junior Member
    Join Date
    Aug 2011
    Posts
    18
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: bit array and register problem

    I finished the code thanks for all your help everyone.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. How to validate username?this my register.jsp
    By surendran610 in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: August 22nd, 2011, 01:42 AM
  2. Replies: 1
    Last Post: May 20th, 2011, 03:58 AM
  3. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM
  4. Cash Register Exercise
    By Consus in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 19th, 2010, 11:52 PM
  5. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM