alpine
asked on
Java program
I have to write a program that takes a command line argument and splits it into it's component parts and prints it out like :
java Prog4 "var1=one+two&var2=this+is +another+o ne"
would yield
var1
one
two
var2
this
is
another
one
I have to do this with three class files that are mostly empty. The problem that I am having is that when I try and run the program, I get an error that is a java.nullpointerexception. The three classes are below. If you have any idea why this is happening, it would be most appreciated if you told me. Thanks!
Here is Prog4.java
/**
* Prog4 is a test harness for parsing out html form queries.
*
* These queries are in the form: name=val1+val2&name2=val3+ val4+val5.
* A query is enterered at the command line as: % java Prog4 "name=val"
* NOTE: do not edit this file.
*
*
*/
class Prog4 {
/**
* The driver for program 4.
*
* @param args the command line argument containing the query to be parsed
*/
public static void main(String[] args) {
if (args.length != 1)
System.err.println("usage: java Prog4 <html form query>");
else {
HTMLFormQuery query = new HTMLFormQuery(args[0]);
for (int i = 0; i < query.getPairCount(); i++) {
HTMLFormQueryPair pair;
pair = query.getPairAt(i);
System.out.println(pair.ge tName());
for (int j = 0; j < pair.getValueCount(); j++)
System.out.print(" " + pair.getValueAt(j));
if (pair.getValueCount() != 0)
System.out.println();
} // for
} // else
} // main
} // Prog4
Here is HTMLformquery.java
class HTMLFormQuery {
private HTMLFormQueryPair[] pairs; // the query pairs
/**
* Returns the number of query pairs
*
* @returns the number of pairs in the query
*/
public int getPairCount() {
return pairs.length;
} // getPairCount
/**
* Returns a requested query pair
*
* @param index the requested pair
*
* @ returns the query pair specified by index
*/
public HTMLFormQueryPair getPairAt(int index) {
return pairs[index];
} // getPairAt
// insert your code below
public HTMLFormQuery(String pairs) {
System.out.println(pairs);
} // constructor
} // HTMLFormQuery
and here is htmlformquerypairs.java
/**
* @author your-name-here
*/
class HTMLFormQueryPair {
private String name; // the name of pair
private String[] values; // the values of the pair
/**
* Returns the name of the query pair
*
* @return the name of the query pair
*/
public String getName() {
return name;
} // getName
/**
* Returns the number of values in the pair
*
* @return the number of values in the pair
*/
public int getValueCount() {
return values.length;
} // getValueCount
/**
* Returns a specified value
*
* @param index the location of the requested value
*
* @return the value from a specified index
*/
public String getValueAt(int index) {
return values[index];
} // getValueAt
// insert your code below __________________________ __________ __________ _____
} // HTMLFormQueryPair
java Prog4 "var1=one+two&var2=this+is
would yield
var1
one
two
var2
this
is
another
one
I have to do this with three class files that are mostly empty. The problem that I am having is that when I try and run the program, I get an error that is a java.nullpointerexception.
Here is Prog4.java
/**
* Prog4 is a test harness for parsing out html form queries.
*
* These queries are in the form: name=val1+val2&name2=val3+
* A query is enterered at the command line as: % java Prog4 "name=val"
* NOTE: do not edit this file.
*
*
*/
class Prog4 {
/**
* The driver for program 4.
*
* @param args the command line argument containing the query to be parsed
*/
public static void main(String[] args) {
if (args.length != 1)
System.err.println("usage:
else {
HTMLFormQuery query = new HTMLFormQuery(args[0]);
for (int i = 0; i < query.getPairCount(); i++) {
HTMLFormQueryPair pair;
pair = query.getPairAt(i);
System.out.println(pair.ge
for (int j = 0; j < pair.getValueCount(); j++)
System.out.print(" " + pair.getValueAt(j));
if (pair.getValueCount() != 0)
System.out.println();
} // for
} // else
} // main
} // Prog4
Here is HTMLformquery.java
class HTMLFormQuery {
private HTMLFormQueryPair[] pairs; // the query pairs
/**
* Returns the number of query pairs
*
* @returns the number of pairs in the query
*/
public int getPairCount() {
return pairs.length;
} // getPairCount
/**
* Returns a requested query pair
*
* @param index the requested pair
*
* @ returns the query pair specified by index
*/
public HTMLFormQueryPair getPairAt(int index) {
return pairs[index];
} // getPairAt
// insert your code below
public HTMLFormQuery(String pairs) {
System.out.println(pairs);
} // constructor
} // HTMLFormQuery
and here is htmlformquerypairs.java
/**
* @author your-name-here
*/
class HTMLFormQueryPair {
private String name; // the name of pair
private String[] values; // the values of the pair
/**
* Returns the name of the query pair
*
* @return the name of the query pair
*/
public String getName() {
return name;
} // getName
/**
* Returns the number of values in the pair
*
* @return the number of values in the pair
*/
public int getValueCount() {
return values.length;
} // getValueCount
/**
* Returns a specified value
*
* @param index the location of the requested value
*
* @return the value from a specified index
*/
public String getValueAt(int index) {
return values[index];
} // getValueAt
// insert your code below __________________________
} // HTMLFormQueryPair
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.