title: How to get data from SocketTCP to save to dataframe in Spark Scala?
body: <blockquote>
  <p>I try to get data from Socket TCP to append to dataframe I recived
  data and executed them to Seq() but when I using forEach to append
  them to dataframe has an problem This is my Code :</p>
  </blockquote>
  <pre><code>object CustomReceiver {
    def main(args: Array[String]): Unit = {
      StreamingExamples.setStreamingLogLevels()
  
      // Create the context with a 1 second batch size
      val spark: SparkSession = SparkSession.builder()
        .master(&quot;local[2]&quot;)
        .appName(&quot;CustomReceiver&quot;)
        .getOrCreate()
  
      val sc = spark.sparkContext
      val ssc = new StreamingContext(sc, Seconds(5))
      import spark.implicits._
  
     /*formatdata line data from Socket:  number1, 20210621090303, RadiusMessage, Stop, 84602496347, v241.66.85.130 */
  
      val linesData1 = ssc.receiverStream(new CustomReceiver(&quot;localhost&quot;, 11000))
      linesData1.flatMap(_.split(&quot; &quot;).map(_.trim))
      linesData1.foreachRDD { rdd =&gt;
        rdd.foreach{ line =&gt; {
  
          val  arrraLine = line.split(&quot;,&quot;).toList
         // oke arrayLine data : List(number1, 20210621090303, RadiusMessage, Stop, 84602496347, 241.66.85.130)
          val testRDD = Seq(arrraLine).map(x =&gt;(x(0), x(1), x(2), x(3), x(4)))
          // oke TestRDD : testRDD :List((number1,20210621090303,RadiusMessage,Stop,84602496347))
  
            val testDF = testRDD.toDF(&quot;cot1&quot;,&quot;cot2&quot;,&quot;cot3&quot;,&quot;cot4&quot;,&quot;cot5&quot;)
           // has an Problem
            testDF.show()
        }
        }
      }
         ssc.start()
          ssc.awaitTermination()
        }
      }
  </code></pre>
  <p>This is my problem when running</p>
  <blockquote>
  <p>java.lang.NullPointerException
  at org.apache.spark.sql.SQLImplicits.localSeqToDatasetHolder(SQLImplicits.scala:231)
  at Cl.CustomReceiver$.$anonfun$main$4(CustomeReceiver.scala:52)
  at Cl.CustomReceiver$.$anonfun$main$4$adapted(CustomeReceiver.scala:45)
  at scala.collection.Iterator.foreach(Iterator.scala:943)
  at scala.collection.Iterator.foreach$(Iterator.scala:943)
  at org.apache.spark.util.CompletionIterator.foreach(CompletionIterator.scala:25)
  at org.apache.spark.rdd.RDD.$anonfun$foreach$2(RDD.scala:1012)
  at org.apache.spark.rdd.RDD.$anonfun$foreach$2$adapted(RDD.scala:1012)
  at org.apache.spark.SparkContext.$anonfun$runJob$5(SparkContext.scala:2236)
  at org.apache.spark.scheduler.ResultTask.runTask(ResultTask.scala:90)
  at org.apache.spark.scheduler.Task.run(Task.scala:131)
  at org.apache.spark.executor.Executor$TaskRunner.$anonfun$run$3(Executor.scala:497)
  at org.apache.spark.util.Utils$.tryWithSafeFinally(Utils.scala:1439)
  at org.apache.spark.executor.Executor$TaskRunner.run(Executor.scala:500)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
  at java.lang.Thread.run(Thread.java:748)</p>
  </blockquote>