简体   繁体   中英

What causes "'void' type not allowed here" error

When I try to compile this:

import java.awt.* ;

    class obj
    {
        public static void printPoint (Point p) 
        { 
            System.out.println ("(" + p.x + ", " + p.y + ")"); 
        }
        public static void main (String[]arg)
        {
            Point blank = new Point (3,4) ; 
            System.out.println (printPoint (blank)) ;
        }
    }

I get this error:

obj.java:12: 'void' type not allowed here
        System.out.println (printPoint (blank)) ; 
                                               ^
1 error

I don't really know how to start asking about this other than to ask:

  • What went wrong here?
  • What does this error message mean?

If a method returns void then there is nothing to print, hence this error message. Since printPoint already prints data to the console, you should just call it directly:

printPoint (blank); 

You are trying to print the result of printPoint which doesn't return anything. You will need to change your code to do either of these two things:

class obj
{
    public static void printPoint (Point p) 
    { 
        System.out.println ("(" + p.x + ", " + p.y + ")"); 
    }
    public static void main (String[]arg)
    {
        Point blank = new Point (3,4) ; 
        printPoint (blank) ;
    }
}

or this:

class obj
{
    public static String printPoint (Point p) 
    { 
        return "(" + p.x + ", " + p.y + ")"; 
    }
    public static void main (String[]arg)
    {
        Point blank = new Point (3,4) ; 
        System.out.println (printPoint (blank)) ;
    }
}

The type problem is that println takes a String to print, but instead of a string, you're calling the printPoint method which is returning void .

You can just call printPoint(blank); in your main function and leave it at that.

您将printPoint()的结果 - 无效 - 传递给println()函数。

printPoint prints by itself rather than returning a string. To fix that call printPoint (blank) without the System.out.println .

A better alternative may be: make printPoint(Point p) return a string (and change its name to something like FormatPoint ), that way the method may be used to format a point for the console, GUI, print, etc rather than being tied to the console.

You probably wanted to do : printPoint (blank); . Looks like you are trying to print twice; once inside printPoint() and once inside main() .

import java.awt.* ;
 
class Main
{
    public static void printPoint (Point p) 
    { 
        System.out.println ("(" + p.x + ", " + p.y + ")"); 
    }
    public static void main (String[]arg)
    {
        Point blank = new Point (3,4) ; 
         printPoint (blank) ;
    }
}

//you can't print the value while its not returnring anything in function try this one

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM