-2

I have this project that I'm doing and for whatever reason, whenever I execute the program and put in the given arguments required for it (that I set and all) and occasionally an IOException is thrown before anything else is executed. It seems to be true because I got loggers everywhere and none of them are being fired. However, it seems that just the loggers are not being fired cause when I look in the json file I output to, it shows that it did do the first step of the execution, just no loggers. I'm new to log4j2 so it may be that but I'm not sure (with the loggers not being fired) but it seems weird that an IOException occurs when it shouldn't at all. Cause when I execute it again right after the crash, it runs just fine.

(Side note: this is in kotlin/jvm, but this is pertaining to the use of the JDK File class)

The exception is thrown here: https://github.com/AlexCouch/projauto/blob/master/src/main/java/thinkingcouch/projauto/Save.kt#L114

I'm on MacOSX High Sierra using Intellij IDEA 2017.3.

Alex Couch
  • 71
  • 8
  • It is possible that the resource is being accessed again by another File object, before having released by the original. You should close all the streams when they are no longer required. – Yamuk Feb 05 '18 at 03:27
  • Time to whip out the debugger :3 – Alex Couch Feb 05 '18 at 03:28
  • Debugger shows the new file variable has a "0" prepended to the path – Alex Couch Feb 05 '18 at 03:29
  • You need to post the code *here*, and *also* the stack trace, but you shouldn't even be calling `File.createNewFile()` here at all. `new FileWriter()` will create a new file for you. At present you are forcing it to delete the file you just created with `createNewFile()`, and create *another* one. – user207421 Feb 05 '18 at 04:16
  • First off, does it even matter? It's github, not some bitly or tinyurl link. Or some unknown website. It's githhub ffs. Secondly, FileWriter is being instantiated after the createNewFile call. so that cannot be the cause. If you look at my previous comment, you'll see that it occasionally prepends "0" to the file path before being appended to the home path I created. I'm gonna debug further. for a visual it looks like this: `Users/username/Projauto/id/0/dirpath` – Alex Couch Feb 05 '18 at 05:06
  • Does what matter? Does a pointless operation that throws an unwanted `IOException` matter? when a subsequent repetition of the same underlying operation succeeds? Of course it matters. Just remove it. – user207421 Feb 05 '18 at 05:33
  • What? No. I was obviously talking about the link thing you made a deal out of. But whatever. I guess that I what I get for coming to stackoverflow right? But anyways, I specified twice what the actual problem is. For some reason, a 0 is being added to the path when it shouldn't be. – Alex Couch Feb 05 '18 at 05:54
  • That means the line `val p = File("$home/$id").toPath().resolve(file).splitPathWithContext(path)` adds the `0` or not? Or what is the value of `$home/$id` which you also use in `val new = File("$home/$id", p.path)` ? – UninformedUser Feb 05 '18 at 07:02
  • I found it. It's my "splitTextWithContext function". I knew it wasn't gonna work but it was working fine up until now. Gotta figure out a replacement for it. – Alex Couch Feb 05 '18 at 09:16
  • `I guess that I what I get for coming to stackoverflow right?`---that's what you get for coming to any forum, disregarding its well-established and [easily discoverable rules](https://stackoverflow.com/help/how-to-ask), and proclaiming they don't matter. – Marko Topolnik Feb 05 '18 at 11:02
  • When did I proclaim the rules don't matter? All I said was that I linked to github which should be a valid way of showing code. But whatever then. I found my answer. We can stop bickering now. – Alex Couch Feb 06 '18 at 08:15

1 Answers1

0

So what ended up happening was I had this function here for isolating a certain part of the given path to be appended to a new path and also saved to json for later use

fun Path.splitPathWithContext(context: String): File{
    val presplit = this.normalize()
    logger.info("presplit: $presplit")
    logger.info("context: $context")
    if(presplit.toString() == context){
        logger.info("Path and context are the same.")
        return presplit.toFile()
    }
    val reg = Pattern.compile("\\b$context\\b")
    val ret = presplit.toString().split(reg)[1]
    logger.info("ret: $ret")
    return File(ret)
}

The solution was to do a strict pattern check against the context variable so that it doesn't cut at a word that contains that string but isn't that string exactly, and it needed to be exact. This solved my issue. No more problems, and no more broken paths, and I also fixed my loggers. I don't know exactly what was causing it to not do any logging, but I fixed it by setting the root level to "all" and then removing all my other logger elements since that's all I needed to do.

Alex Couch
  • 71
  • 8