I am playing around with Tomcat authentication using Mysql. To facilitate this I’m trying to model the Tomcat user data stored in the database. So I built a TomcatUser class.
I’ve stored the user name and password as Strings to just make life easy during testing. Security will be implemented later. And the user’s Tomcat roles are stored in an ArrayList so I can easily search and manage the user’s roles. And I have a few constructors for different scenarios.
One of these is using a String VARARG to set however many Tomcat roles the user may have. I know that the parameters in a VARARG are stored in an array. In this case a String Array. I use the Collections.addAll() method to add the contents of the VARARG array to the object’s ArrayList.
In the test class I’m getting a NullPointerException from instantiating an Iterator from the object’s , probably for some silly mistake on my part. But then I’m new. Below is my code, for both the model class and the test. Can you tell me what I’m doing wrong? And any tips or advice in general would be appreciated.
The model.
/*
* Class modeling user data for Tomcat.
*/
package tomcat;
import java.util.ArrayList;
import java.util.Collections;
public class TomcatUser
{
String tomcatUserName; //Field to store Tomcat user's name.
String tomcatUserPassword; //Field to store Tomcat user's password.
/*
* Field to store the Tomcat user's roles as an ArrayList. This allows for easy element checking.
*/
ArrayList<String> tomcatUserRoles;
/*
* Constructor intended for creating a roleless user. Not much use, but who knows.
*/
public TomcatUser(String tomcatUserName, String tomcatUserPassword)
{
super();
this.setTomcatUserName(tomcatUserName);
this.setTomcatUserPassword(tomcatUserPassword);
}
/*
* Constructors for instantiating for general use.
* Three constructors are provided to provide options for instantiation.
*/
public TomcatUser(String tomcatUserName, ArrayList<String> tomcatUserRoles)
{
super();
this.setTomcatUserName(tomcatUserName);
this.setTomcatUserRoles(tomcatUserRoles);
}
public TomcatUser(String tomcatUserName, String... tomcatUserRoles)
{
super();
this.setTomcatUserName(tomcatUserName);
}
/*
*
*/
/*
* Constructors intended for adding a new user to the database.
* Three constructors are provided to provide options for instantiation.
*/
public TomcatUser(String tomcatUserName, String tomcatUserPassword, ArrayList<String> tomcatUserRoles)
{
this(tomcatUserName, tomcatUserPassword);
this.setTomcatUserRoles(tomcatUserRoles);
}
/*
*
*/
/*
* Standard setters and getters.
*/
/**
* @return the tomcatUserName
*/
public String getTomcatUserName() {
return tomcatUserName;
}
/**
* @param tomcatUserName the tomcatUserName to set
*/
public void setTomcatUserName(String tomcatUserName) {
this.tomcatUserName = tomcatUserName;
}
/**
* @return the tomcatUserPassword
*/
public String getTomcatUserPassword() {
return tomcatUserPassword;
}
/**
* @param tomcatUserPassword the tomcatUserPassword to set
*/
public void setTomcatUserPassword(String tomcatUserPassword) {
this.tomcatUserPassword = tomcatUserPassword;
}
/**
* @return the tomcatUserRoles
*/
public ArrayList<String> getTomcatUserRoles() {
return tomcatUserRoles;
}
/**
* @param tomcatUserRoles the tomcatUserRoles to set
*/
public void setTomcatUserRoles(ArrayList<String> tomcatUserRoles) {
this.tomcatUserRoles = tomcatUserRoles;
}
/*
*
*/
/*
* Setter to handle input options from matching constructors, and to provide options for setting tomcatUserRoles field.
*/
public void setTomcatUserRoles(String[] newTomcatUserRoles)
{
Collections.addAll(this.tomcatUserRoles, newTomcatUserRoles);
}
/*
*
*/
/*
* Method to add a role to the object.
*/
public void addTomcatUserRole (String newTomcatUserRole)
{
this.tomcatUserRoles.add(newTomcatUserRole);
}
public Boolean hasTomcatRole (String checkTomcatRole)
{
return this.tomcatUserRoles.contains(checkTomcatRole);
}
}
And the test class.
package tomcat;
import java.util.ArrayList;
import java.util.Iterator;
public class TomcatUserTest
{
static ArrayList<String> testTomcatRoles = new ArrayList<String>();
/**
* @param args
*/
public static void main(String[] args)
{
for (Integer iterator = 1; iterator <4 ; iterator++)
{
testTomcatRoles.add("testTomcatRole" + iterator.toString());
}
TomcatUser testTomcatUser01 = new TomcatUser("testUser01", "testPassword01");
System.out.print("testTomcatUser01 contents: ");
System.out.print(testTomcatUser01.getTomcatUserName() + " " + testTomcatUser01.getTomcatUserPassword());
System.out.println();
TomcatUser testTomcatUser02 = new TomcatUser("testUser02", testTomcatRoles);
System.out.print("testTomcatUser02 contents: ");
System.out.print(testTomcatUser02.getTomcatUserName() + " ");
Iterator<String> testTomcatUser02Iterator = testTomcatUser02.getTomcatUserRoles().iterator();
while (testTomcatUser02Iterator.hasNext())
{
System.out.print(testTomcatUser02Iterator.next());
if (testTomcatUser02Iterator.hasNext())
{
System.out.print(" ");
}
}
System.out.println();
TomcatUser testTomcatUser03 = new TomcatUser("testTomcatUser03", "testPassword03", testTomcatRoles);
System.out.print("testTomcatUser03 contents: ");
System.out.print(testTomcatUser03.getTomcatUserName() + " " + testTomcatUser03.getTomcatUserPassword() + " ");
Iterator<String> testTomcatUser03Iterator = testTomcatUser03.getTomcatUserRoles().iterator();
while (testTomcatUser03Iterator.hasNext())
{
System.out.print(testTomcatUser03Iterator.next());
if (testTomcatUser03Iterator.hasNext())
{
System.out.print(" ");
}
}
System.out.println();
TomcatUser testTomcatUser04 = new TomcatUser("testTomcatUser04", "testRole1", "testRole2", "testRole3");
System.out.print("testTomcatUser04 contents: ");
System.out.print(testTomcatUser04.getTomcatUserName() + " ");
Iterator<String> testTomcatUser04Iterator = testTomcatUser04.getTomcatUserRoles().iterator();
while (testTomcatUser04Iterator.hasNext())
{
System.out.print(testTomcatUser04Iterator.next());
if (testTomcatUser04Iterator.hasNext())
{
System.out.print(" ");
}
}
}
}
This constructor:
never assigns the field
tomcatUserRoles.