TypeError: __init__() missing 1 required positional argument: 'name'

1、错误描述

Hello World
Traceback (most recent call last):
  File "G:\NetBeansWork\Color\src\color.py", line 15, in 
    stu = Student();
TypeError: __init__() missing 1 required positional argument: 'name'
Annotation: Was parsing G:\NetBeansWork\Color\src\color.py
Annotation: Was parsing G:\NetBeansWork\Color\src\color.py
Was parsing G:\NetBeansWork\Color\src\color.py
Was parsing G:\NetBeansWork\Color\src\color.py
Caused: java.lang.IllegalArgumentException: Cannot create PyString with non-byte value
	at org.python.core.PyString.(PyString.java:64)
	at org.python.core.PyString.(PyString.java:70)
	at org.python.antlr.GrammarActions.extractStrings(GrammarActions.java:477)
	at org.python.antlr.PythonParser.atom(PythonParser.java:10996)
	at org.python.antlr.PythonParser.power(PythonParser.java:10363)
	at org.python.antlr.PythonParser.factor(PythonParser.java:10290)
	at org.python.antlr.PythonParser.term(PythonParser.java:9855)
	at org.python.antlr.PythonParser.arith_expr(PythonParser.java:9581)
	at org.python.antlr.PythonParser.shift_expr(PythonParser.java:9308)
	at org.python.antlr.PythonParser.and_expr(PythonParser.java:9141)
	at org.python.antlr.PythonParser.xor_expr(PythonParser.java:8978)
	at org.python.antlr.PythonParser.expr(PythonParser.java:8814)
	at org.python.antlr.PythonParser.comparison(PythonParser.java:8360)
	at org.python.antlr.PythonParser.not_test(PythonParser.java:8287)
	at org.python.antlr.PythonParser.and_test(PythonParser.java:8073)
	at org.python.antlr.PythonParser.or_test(PythonParser.java:7911)
	at org.python.antlr.PythonParser.test(PythonParser.java:7771)
	at org.python.antlr.PythonParser.testlist_gexp(PythonParser.java:11252)
	at org.python.antlr.PythonParser.atom(PythonParser.java:10619)
	at org.python.antlr.PythonParser.power(PythonParser.java:10363)
	at org.python.antlr.PythonParser.factor(PythonParser.java:10290)
	at org.python.antlr.PythonParser.term(PythonParser.java:9855)
	at org.python.antlr.PythonParser.arith_expr(PythonParser.java:9581)
	at org.python.antlr.PythonParser.shift_expr(PythonParser.java:9308)
	at org.python.antlr.PythonParser.and_expr(PythonParser.java:9141)
	at org.python.antlr.PythonParser.xor_expr(PythonParser.java:8978)
	at org.python.antlr.PythonParser.expr(PythonParser.java:8814)
	at org.python.antlr.PythonParser.comparison(PythonParser.java:8360)
	at org.python.antlr.PythonParser.not_test(PythonParser.java:8287)
	at org.python.antlr.PythonParser.and_test(PythonParser.java:8073)
	at org.python.antlr.PythonParser.or_test(PythonParser.java:7911)
	at org.python.antlr.PythonParser.test(PythonParser.java:7771)
	at org.python.antlr.PythonParser.printlist(PythonParser.java:3784)
	at org.python.antlr.PythonParser.print_stmt(PythonParser.java:3592)
	at org.python.antlr.PythonParser.small_stmt(PythonParser.java:2791)
	at org.python.antlr.PythonParser.simple_stmt(PythonParser.java:2524)
	at org.python.antlr.PythonParser.stmt(PythonParser.java:2438)
	at org.python.antlr.PythonParser.suite(PythonParser.java:7652)
	at org.python.antlr.PythonParser.funcdef(PythonParser.java:1506)
	at org.python.antlr.PythonParser.compound_stmt(PythonParser.java:6218)
	at org.python.antlr.PythonParser.stmt(PythonParser.java:2457)
	at org.python.antlr.PythonParser.suite(PythonParser.java:7652)
	at org.python.antlr.PythonParser.classdef(PythonParser.java:13225)
	at org.python.antlr.PythonParser.compound_stmt(PythonParser.java:6232)
	at org.python.antlr.PythonParser.stmt(PythonParser.java:2457)
	at org.python.antlr.PythonParser.file_input(PythonParser.java:618)
[catch] at org.netbeans.modules.python.source.PythonParser.parse(PythonParser.java:344)
	at org.netbeans.modules.python.source.PythonParser.parse(PythonParser.java:148)
	at org.netbeans.modules.parsing.impl.TaskProcessor.callParse(TaskProcessor.java:598)
	at org.netbeans.modules.parsing.impl.SourceCache.getResult(SourceCache.java:228)
	at org.netbeans.modules.parsing.impl.TaskProcessor$RequestPerformer.run(TaskProcessor.java:775)
	at org.openide.util.lookup.Lookups.executeWith(Lookups.java:279)
	at org.netbeans.modules.parsing.impl.TaskProcessor$RequestPerformer.execute(TaskProcessor.java:702)
	at org.netbeans.modules.parsing.impl.TaskProcessor$CompilationJob.run(TaskProcessor.java:663)
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:514)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:1418)
	at org.netbeans.modules.openide.util.GlobalLookup.execute(GlobalLookup.java:45)
	at org.openide.util.lookup.Lookups.executeWith(Lookups.java:278)
	at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:2033)

2、错误原因

# To change this license header, choose License Headers in Project Properties.
# To change this template file, choose Tools | Templates
# and open the template in the editor.

if __name__ == "__main__":
    print("Hello World")
#encoding=utf-8
class Student:
    def __init__(self,name):
        self.name = name;
    
    def showStudent(self):
        print("name:%s"%self.name);

stu = Student();
stu.showStudent("zhangsan");

3、解决办法

      实例化对象需要传入一个name参数

if __name__ == "__main__":
    print("Hello");

class Student:
    def __init__(self,name):
        self.name = name;

    def showStudent(self,name):
        print("name:%s"%self.name);

stu = Student("zhangsan");
stu.showStudent("lisi");

 

你可能感兴趣的:(Python,NetBeans,python)