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

Thread: Why when I have successfully added my parts, the output is null and false?

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

    Default Why when I have successfully added my parts, the output is null and false?

    import java.util.ArrayList;
    public class BikePartsDB {
    private static ArrayList<BikeParts> bikepartsList = new ArrayList<BikeParts>();

    public static void showBikePartsMenu() {
    //Display the menu
    System.out.println("Option 1: View all Bike Parts");
    System.out.println("Option 2: Add a new Bike Parts");
    System.out.println("Option 3: Delete a Bike Parts");
    System.out.println("Option 4: Exit");
    }

    public static void addBike(BikeParts bk) {
    //add some data to the list
    bikepartsList.add(bk);
    if (bikepartsList.size() != 0) {
    System.out.println("Successfully added!");
    }
    else {
    System.out.println("Add fail!");
    }
    }


    public static void viewAllBikeParts() {

    if (bikepartsList.size() == 0) {
    System.out.println("No bike parts.");
    }

    else {
    //display all Customers in the list
    String output = String.format("%-10s %-10s %-10s %-10s\n", "NO.", "PARTNAME", "DESCRIPTION", "AVAILABILITY");
    for (int i = 0; bikepartsList.size() > i; i++) {
    output += String.format("%-10d %-10s %-10s %-10s\n", i + 1, bikepartsList.get(i).getPartName(), bikepartsList.get(i).getDescription(), bikepartsList.get(i).isAvailable());
    }
    System.out.println(output);
    }

    }

    public static void delBike(int BikepartsID) {
    //clean up the data in the list
    bikepartsList.remove(BikepartsID - 1);
    }
    public static void processOption(int subOption) {

    while (subOption != 4) {
    showBikePartsMenu();
    subOption = Helper.readInt("Enter option > ");
    if (subOption == 1) {
    // View all bike part
    viewAllBikeParts();


    } else if (subOption == 2) {
    // Add a bike part
    BikeParts newBikeparts = inputBikePartsToAdd();

    addBike(newBikeparts);




    } else if (subOption == 3) {
    // Delete a bike part
    viewAllBikeParts();
    int delete = selectBikePartsToDelect1();
    int size = bikepartsList.size();
    delBike(delete);
    if (size != bikepartsList.size()) {
    System.out.println("Successfully deleted!");
    }
    else {
    System.out.println("Delete unsuccessful");
    }

    }else if (subOption == 4) {
    System.out.println("End of tasks");
    break;
    }
    //invalid option
    else {
    System.out.println("Invalid type");
    }
    }

    //show the menu again & ask for option
    showBikePartsMenu();


    }



    //==========Option 1 ==============



    //==========Option 2 ==============
    public static BikeParts inputBikePartsToAdd() {
    String partName = Helper.readString("Enter Part Name > ");
    String description = Helper.readString("Enter Description > ");
    boolean isAvailable = Helper.readBoolean("Enter Availability > ");

    BikeParts newBikeparts = new BikeParts(partName, description, isAvailable);
    return newBikeparts;
    //request user for the bike part to add to the list
    }



    //==========Option 3 ==============
    public static int selectBikePartsToDelect1() {
    //request the user to select bike part for process

    int number = Helper.readInt("Enter NO. to delete > ");
    return number;

    }

    public static void delBikeParts(int bikepartsID) {
    //remove bike part to list

    }

    public static void populateBikePartsDB() {
    // TODO Auto-generated method stub

    }

    public static void cleanupBikePartsDB() {
    // TODO Auto-generated method stub

    }

    public static void removeAll() {
    // TODO Auto-generated method stub

    }

    public static Object getSize() {
    // TODO Auto-generated method stub
    return null;
    }




    }

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

    Default Re: Why when I have successfully added my parts, the output is null and false?

    Can you post the program's output that shows what you are talking about?

    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
    Aug 2020
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why when I have successfully added my parts, the output is null and false?

    This is my output:

    Option 1: View all Bike Parts
    Option 2: Add a new Bike Parts
    Option 3: Delete a Bike Parts
    Option 4: Exit
    Enter option > 2
    Enter Part Name > wheels
    Enter Description > bike wheels
    Enter Availability > true
    Successfully added!
    Option 1: View all Bike Parts
    Option 2: Add a new Bike Parts
    Option 3: Delete a Bike Parts
    Option 4: Exit
    Enter option > 1
    NO. PARTNAME DESCRIPTION AVAILABILITY
    1 null null false

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

    Default Re: Why when I have successfully added my parts, the output is null and false?

    Is this the line that is getting the bad values?
     bikepartsList.get(i).getDescription(), bikepartsList.get(i).isAvailable());
    Where is the definition of the BikeParts 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.

  5. #5
    Junior Member
    Join Date
    Aug 2020
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why when I have successfully added my parts, the output is null and false?

    I am not sure which part is causing my output to have an error.
    this is my bikeparts class:
     
     
    public class BikeParts{
     
    		//declare class parameters
    	private String partName;
    	private String description;
    	private boolean isAvailable;
     
     
    		public BikeParts(String partName, String description, boolean isAvailable) {
    			//initialize paramater
     
    		}
    		public String getPartName() {
    			return partName;
    		}
    		public void setPartName(String partName) {
    			this.partName = partName;
    		}
    		public String getDescription() {
    			return description;
    		}
    		public void setDescription(String description) {
    			this.description = description;
    		}
    		public boolean isAvailable() {
    			return isAvailable;
    		}
    		public void setAvailable(boolean isAvailable) {
    			this.isAvailable = isAvailable;
    		}
     
    		public String toString() {
    	        String x = "" + this.isAvailable;
    	        return x;
    	    }
     
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why when I have successfully added my parts, the output is null and false?

    Where are values assigned to the 3 variables in the BikeParts class?
    Are the assignment statements ever executed so that the variables are given values?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Aug 2020
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Why when I have successfully added my parts, the output is null and false?

    Do you mean something like this?

    import static org.junit.Assert.*;
     
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    import java.util.ArrayList;
     
    public class BikePartsTest {
     
    	private BikeParts bp1;
    	private BikeParts bp2;
     
    	@Before
    	public void setUp() throws Exception {
    		bp1 = new BikeParts("Wheels", "Bike Wheels", true);
    		bp2 = new BikeParts("Handle", "Bike Handles", true);
    	}
     
    	@Test
    	public void c206_test() {
     
    		assertTrue("C206_CaseStudy_SampleTest ",true);
    	}
     
    	@Test
     
    	public void checkForArrayList() {
    		assertEquals("checks if arrayList size is 0, if there is means an arraylist is present or else there will be another error.", 0, BikePartsDB.getSize());
    	}
     
    	@Test
     
    	public void addBikeTest() {
    		BikePartsDB.addBike(bp1);
    		BikePartsDB.addBike(bp2);
    		assertEquals("If the size is 2 after adding, means successful added.", 2, BikePartsDB.getSize());
    	}
     
     
     
    	@Test	
     
    	public void viewBikeTest() {
     
    		//checks if there is an arraylist with the size
    		checkForArrayList();
     
    		//checks for output first if no customer added
     
     
     
    		//checks if arraylist is there and also tests if adding is working
    		addBikeTest();
     
    		String output = String.format("%-10s %-10s %-15s %-15s\n", "NO.", "PARTNAME", "DESCRIPTION", "AVAILABILITY");
    		output += String.format("%-10d %-10s %-15s %-15d\n", 1, "Wheels", "Bike Wheels", true);
    		output += String.format("%-10d %-10s %-15s %-15d\n", 2, "Handles", "Bike Handles", true);
     
     
     
    	}
     
     
    	@Test
     
    	public void deleteBikeTest() {
    		//add and check firsts
    		checkForArrayList();
    		addBikeTest();
    		BikePartsDB.delBike(2);
    		assertEquals("Check if array size is 1. if its 1 means 1 of them has been deleted.", 1, BikePartsDB.getSize());
    		BikePartsDB.delBike(1);
    		assertEquals("Check if array size is 0. if its 0 means it has been deleted.", 0, BikePartsDB.getSize());
     
    	}
     
     
    	@After
    	public void tearDown() throws Exception {
     
    		bp1 = null;
    		bp2 = null;
    		BikePartsDB.removeAll();
    	}
     
    }

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Why when I have successfully added my parts, the output is null and false?

    Sorry, I do not see any where in that code where values are assigned to the variables in the BikeParts class. The assignment statement needs to be in the BikeParts class. Something like this:
       partName = value;   // assign value to partName
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: March 23rd, 2014, 10:43 AM
  2. [SOLVED] "null" in my output :( Scanner problem?
    By crys in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 2nd, 2012, 09:05 PM
  3. [SOLVED] Program is running successfully but still getting error message..
    By sumit043020701 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 24th, 2011, 12:26 AM
  4. Replies: 1
    Last Post: February 19th, 2011, 06:08 PM