简体   繁体   English

如何解决Function1中的Function1(上下文)不能应用于()

[英]How to solve Function1 (Context) in Function1 cannot be applied to ()

I have the following problems: 我有以下问题:

I would like to call a function from another class so I added this line of code 我想从另一个类调用一个函数,所以我添加了这一行代码

Function1 func = new Function1(); and I get an error saying 我收到一个错误消息

Function1 (Context) in Function1 cannot be applied to () Function1中的Function1(上下文)不能应用于()

Furthermore, relating to this function and its error, I intend calling the aforementioned function which takes a JSON object and a Filename as parameters and it returns a file, however, when I enter it, I get the following error 此外,关于此函数及其错误,我打算调用上述函数,该函数以JSON对象和Filename作为参数,并返回一个文件,但是,当我输入它时,出现以下错误

Wrong 2nd argument type, found Java.lang.String required Java.io.File

The code in question is this: 有问题的代码是这样的:

JSONObject export = jsonArray1.getJSONObject(index);

 File file = func.exportToFile(export, "Export.json");

The fuction in question starts like this: 有问题的功能是这样开始的:

public void exportToFile(JSONObject objectToExport, File fN)
    {
        String output = objectToExport.toString();
        file_ = fN;
        if (!file_.exists()) {
            try {
                file_.createNewFile();
               } catch (IOException e) {
            e.printStackTrace();
        }
    }
    try{
        FileOutputStream fOut = new FileOutputStream(file_);
        fOut.write(output.getBytes());
        fOut.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

NB: I have tried to call the function like this: 注意:我试图这样调用该函数:

File file = func.exportToFile(export, func.file); 文件文件= func.exportToFile(export,func.file);

but I only get the error saying incompatible types 但我只收到错误消息,指出类型不兼容

Required Java.io.file 必需的Java.io.file

Found Void 发现虚空

What have I done wrong? 我做错了什么?

this func.exportToFile(export, func.file); 这个func.exportToFile(export, func.file); will not return anything since exportToFile it's a void method . 由于exportToFile是空方法,因此不会返回任何内容。

change your method to make it return file this way : 更改您的方法,使其以这种方式返回文件:

public File exportToFile(JSONObject objectToExport, File fN) {
  String output = objectToExport.toString();
  file_ = fN;
  if (!file_.exists()) {
        try {
          file_.createNewFile();
        } catch (IOException e) {
          e.printStackTrace();
        }
  }
  try{
    FileOutputStream fOut = new FileOutputStream(file_);
    fOut.write(output.getBytes());
    fOut.close();
    return file_;
  } catch (FileNotFoundException e) {
    e.printStackTrace();
  } catch (IOException e) {
    e.printStackTrace();
  }
  return null;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Kotlin 找不到符号 Function1 构建错误 - Kotlin cannot find symbol Function1 build error 如何在Java中实现Function1(其compose和andThen方法)? - How to implement a Function1 in Java (its compose and andThen methods)? Java中的finagle-Function,Function1等 - finagle in java - Function, Function1 etc Scala 功能1<t,u> 变成 Function1<object,object> 在 Java</object,object></t,u> - Scala Function1<T,U> becomes Function1<Object,Object> in Java Java 相当于 Kotlin 的 Function0 和 Function1 是什么? - What is Java equivalent to Function0 and Function1 of Kotlin? 如何在Spark 2.3的数据集中开发Function1 / MapFunction接口的实现 <Row> 被突变 - How to develop implementation of the Function1/MapFunction interfaces in Spark 2.3 where the Dataset<Row> are mutated 为什么我不能将Scala的Function1隐式转换为java.util.function.Function? - Why can't I implicit convert Scala's Function1 to java.util.function.Function? 为什么 vavr 的 Function1 不接受 Seq<parent> class 接受序列<child> ?</child></parent> - Why won't vavr's Function1 accepting Seq<Parent> class accept Seq<Child>? WebDriver无法对以onclick = function1()为属性的输入元素执行单击 - WebDriver is unable to perform click on an input element having onclick=function1() as an attribute 获取 Akka stream 代码错误“主”线程 java.lang.NoClassDefFoundError: scala/Function1$class - Error getting Akka stream code Exception in thread “main” java.lang.NoClassDefFoundError: scala/Function1$class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM