Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7722195
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:06:32+00:00 2026-06-01T04:06:32+00:00

I recently posted a question about getting a NullPointerException whenever I called an array

  • 0

I recently posted a question about getting a NullPointerException whenever I called an array of objects. I have traced the problem back to some disconnect between the main method providing the data and the method in question (Team.sortPlayers()) receiving the data.

public class Project3 {


public static void main(String[] args) {
Input3 input = new Input3();
Team teams[] = new Team[input.NUMBER_OF_TEAMS];
Player players[] = new Player[input.NUMBER_OF_PLAYERS];
String playas[] = new String[input.NUMBER_OF_PLAYERS];
String temp;
String name;

for ( int i=0 ; i<input.NUMBER_OF_TEAMS ; i++ ) {
    name = input.getNextString();
    System.out.println(name);
    for ( int j=0 ; j<input.NUMBER_OF_PLAYERS ; j++ )
    {playas[j] = input.getNextString();
        System.out.println(playas[j]);}
    teams[i] = new Team(name, playas); //THIS LINE SENDS OVER THE DATA TO THE QUESTIONABLE METHOD
    teams[i].sortPlayers();
    System.out.println(teams[i]);
    }
}
}

//------------------------------
//
//------------------------------

class Player {

public String[] name;

public Player(String inputname) {

    name = inputname.split(" ");

}
public String[] getName() {
    return name;
}

public String getFirstName() {
    return name[0];
}

public String getLastName() {
    String last = name[1];
    return last;
}
}

//-----------------------
//
//-----------------------

class Team {
private String teamname;
public Player players[];
public Player temp;

public Team(String inputname, String plays[]) { //THIS METHOD RECEIVES A NULL FOR 'INPUTNAME' AND WHAT APPEARS TO BE A JIBBERISH (ex: Player@5bdf59bd, maybe a memory address?) FOR 'PLAYS[]'

    inputname = teamname;
    System.out.println(teamname);    
    players = new Player [plays.length];
    for( int k=0 ; k<plays.length ; k++ )
    {   System.out.println(inputname);
        this.players[k] = new Player(plays[k]);
    System.out.println(players[k]);
    }

}

public void sortPlayers() {
    int n = players.length;

    for (int pass=1; pass < n; pass++){
        for (int i=0; i < n-pass; i++) {
            String playerName = players[i].getLastName();
            String nextPlayerName = players[i+1].getLastName();
            if(playerName.compareTo(nextPlayerName) > 0)
            temp = players[i];
        players[i] = players[i+1];
        players[i+1] =  temp;
        }
    }
}
}

If anyone could help me figure what is going on here, I’d be very grateful! I’ve marked the two problematic statements with comments, and a PasteBin of it all can be found below:
http://pastebin.com/QGALKbP6

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T04:06:34+00:00Added an answer on June 1, 2026 at 4:06 am

    “temp” should not be a member variable, it should be local to the if block in the sort method. and, because if its expanded scope, you are inadvertently clearing some members of your players array due to the fact that your “if” block is missing some braces.

            if(playerName.compareTo(nextPlayerName) > 0) {
              Player temp = players[i];
              players[i] = players[i+1];
              players[i+1] =  temp;
            }
    

    generally, even though braces are optional in some circumstances, you should use them all the time to avoid subtle bugs like this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently posted a question about getting last 3 results in table in the
I recently posted a question here about some memory issues I was having. I've
I recently posted a question about Azure... is it really an OS? I understand
I recently posted a question about centering a page with CSS. I figured out
I have recently posted another question which straight away users pointed me in the
I recently posted a question about optimizing the algorithm to compute the Levenshtein Distance,
As some may be aware, I recently posted about high memory usage on my
Recently i posted a question about what the variable is of a file which
Recently I posted a question about the html helper dropdownlist and got it working
I've recently posted a general question about RAII at SO . However, I still

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.