# How can I solve "void cannot be dereferenced" error?

• 04-05-2010, 06:43 PM
fervent07
How can I solve "void cannot be dereferenced" error?
Hi everbody,

We have a homework.The question is below.

"Define a class Rational, which in addition
to the constructor and the accessor methods Enumerator and Denominator has methods
printRational (which print a rational number) as well as getSum, and getProduct,
which generate the sum and the product of rational numbers and return the corresponding
object of type Rational. For instance, if we generate rational numbers Rational
r1 = new Rational(1,2); and Rational r2 = new Rational(3,7); then r1.print()
should print 1/2. r2.getProduct(r1).print(); should print 3/14 (which corresponds
to the product of r2 and r1), and r2.getSum(r1).print(); should print 13/14 (which
corresponds to the sum of r2 and r1)."

I wrote like this

Code:

```public class Rational{                 int enumerator;         int denominator;                 Rational(int e, int d){                                 enumerator=e;                 denominator=d;         }                 public void  getSum(Rational r){                 enumerator=enumerator+r.enumerator;                 denominator=denominator+r.denominator;         }                 public void getProduct(Rational r){                 enumerator=enumerator*r.enumerator;                 denominator=denominator*r.denominator;         }                 public String printRational(){                 String output=enumerator + "/" + denominator;                 return output;         }                 public static void main (String[] args) {                                 Rational r1=new Rational(1, 2);                 Rational r2=new Rational(3, 7);                         System.out.println(r1.printRational());                 System.out.println(r2.printRational());                                 r2.getSum(r1).printRational();                 r2.getProduct(r1).printRational();                         }         }```
But i get some errors.How can i fix these?

C:\Users\aCid\Desktop\Java Docs\HomeWorks\Rational.java:35: void cannot be dereferenced
r2.getSum(r1).printRational();
^
C:\Users\aCid\Desktop\Java Docs\HomeWorks\Rational.java:36: void cannot be dereferenced
r2.getProduct(r1).printRational();
^
2 errors